If I am correct, when you do: double x=0; while(x<1000000000) { x = x+1; } C++ compiles the + to double operation. In Pharo, this code: |x| x := 0. 1 to: 1000000000 do:[:each| x := x +1] is using SmallInteger operations. The code you wrote is fully inlined in Cog's JIT (no message sends). Hence you're comparing double operation versus int operations. That's why Pharo is faster. Pharo should be a little bit slower than C++ if it would use int32 because of the 4 Smallinteger checks it requires for the inlined <= and the 2 inlined +. I am a bit surprised the C++ compiler does not entirely remove the loop as it is side-effect free. Are you compiling in O2 ? For *, firstly you're comparing this time double arithmetic vs double arithmetic, where Pharo is slower due to boxing/unboxing, and secondly Cogit does not inline the *, so there is a message send in the loop in Pharo, leading to worse performance. Are you benchmarking on Pharo 64 bits ? Because this kind of double benchmarks should be way faster on the 64 bits VM than the 32 bits one. Anyway, for benchmarking we normally use the bench suite here: http://squeak.org/codespeed/ and micro-benchmarks are always questionable. On Sat, Dec 17, 2016 at 1:41 PM, Dimitris Chloupis <kilon.alios@gmail.com> wrote:
in multiplication pharo is around 2 times slower compared to C++
#include <stdio.h>
int main() { double x=1; for(double i; i<1000000000 ; ++i) { x = 0.1*i; }
return 1; }
time ./testMul 3.13 real 3.13 user 0.00 sys
time ./pharo Ephestos.image eval "|x| x := 1. 1 to: 1000000000 do:[:each| x := 0.1 * each]" 1 4.97 real 4.48 user 0.09 sys
On Sat, Dec 17, 2016 at 2:16 PM Dimitris Chloupis <kilon.alios@gmail.com> wrote:
So I was bored and decided to test how fast pharo is compared to C++.
so I tested addition
C++ version:
#include <stdio.h>
int main() { double x=0; while(x<1000000000) { x = x+1; }
return 1; }
time ./test1 2.84 real 2.84 user 0.00 sys
Pharo version:
time ./pharo Ephestos.image eval "|x| x := 0. 1 to: 1000000000 do:[:each| x := x +1]" 1 2.09 real 1.94 user 0.08 sys
Pharo is +50% faster than C++ ... o_O ... that's suprising, I assume here Pharo VM probably does some kind of optimisation.