no its not the loop i tried changing the computation and printing the sum the loops is definetly executed and I am also getting
*time_spent=0.000001* ok this is insane but i found what is wrong with your code your loop is for (long i = 1; i <= 1000000000; ++i) but my loop is for (int i = 0; i <= 1000000000; ++i) no its not the long that causes the problem its the fact that you star counting from 1 no idea why but if you start counting from 0 it reports sensible numbers in the case of long sum = 0; for (long i = 0; i <= 1000000000; ++i) { sum = 0+i; } it reports the reasonable *time_spent=0.397446* so maybe its some optimisation it does when the counting starts from 1 not executing the loop in its entirety ? On Sat, Dec 17, 2016 at 6:33 PM Sven Van Caekenberghe <s...@stfx.eu> wrote: > On 17 Dec 2016, at 15:52, Sven Van Caekenberghe <s...@stfx.eu> wrote: > >> >> On 17 Dec 2016, at 15:23, Sven Van Caekenberghe <s...@stfx.eu> wrote: >> >> >>> On 17 Dec 2016, at 14:28, Henrik Nergaard <henrik.nerga...@uia.no> wrote: >>> >>> This is not a comparable benchmark as the sum in Smalltalk keeps on growing and ends up doing LargeInteger calculations and thus the speed loss, while the c the int just rolls over. >>> >>> Sum in Smalltalk = 500000000500000000 >>> Sum in C = -1243309312 >> >> Yes, I realised afterwards. Shame on me. >> >> Now, in Pharo 64 bits: >> >> [ >> | sum | >> sum:= 0. >> 1 to: 1e9 do: [ :i | sum := sum + i ]. >> self assert: sum = (1e9 * (1e9 + 1) / 2) ] timeToRun. >> >> "0:00:00:03.379" >> >> (1e9 * (1e9 + 1) / 2) < SmallInteger maxVal. >> >> "true" >> >> So there the speed difference is way smaller. > > And here is a correct C99 version > > $ cat bench.c > #include <stdio.h> > #include <time.h> > #include <assert.h> > > int main() { > clock_t begin = clock(); > > long sum = 0; > for (int i = 1; i <= 1000000000; i++) > sum += i; > > assert(sum == ((1000000000*1000000001)/2)); > > clock_t end = clock(); > double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; > printf("time_spent=%f\n", time_spent); > } > > prometheus:tmp sven$ ./a.out > time_spent=2.393986 But, $ cat bench.c #include <stdio.h> #include <time.h> #include <assert.h> int main() { clock_t begin = clock(); long sum = 0L; for (long i = 1; i <= 1000000000; i++) sum += i; assert(sum == ((1000000000L*1000000001L)/2L)); clock_t end = clock(); double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("time_spent=%f\n", time_spent); return (int)sum; } $ cc -O2 bench.c $ ./a.out time_spent=0.000002 Who knows what optimisation was applied ? Maybe the loop wasn't executed at all ... (but I did add the return statement). >>> Best regards, >>> Henrik >>> >>> -----Original Message----- >>> From: Pharo-users [mailto:pharo-users-boun...@lists.pharo.org] On Behalf Of Sven Van Caekenberghe >>> Sent: Saturday, December 17, 2016 2:15 PM >>> To: Any question about pharo is welcome <pharo-users@lists.pharo.org> >>> Subject: Re: [Pharo-users] [Stupid Benchmarks] C++ vs Pharo >>> >>> Like this: >>> >>> [ | sum | sum:= 0. 1 to: 1e9 do: [ :i | sum := sum + i ]. sum ] timeToRun. >>> >>> "0:00:00:39.305" >>> >>> cat bench.c >>> >>> #include <stdio.h> >>> #include <time.h> >>> >>> int main() { >>> clock_t begin = clock(); >>> >>> int sum = 0; >>> for (int i = 0; i < 1000000000; i++) >>> sum += i; >>> >>> clock_t end = clock(); >>> double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; >>> printf("time_spent=%f\n", time_spent); } >>> >>> ./a.out >>> >>> time_spent=2.370234 >>> >>> That is C being 16 times faster ... >>> >>>> On 17 Dec 2016, at 13:49, Sven Van Caekenberghe <s...@stfx.eu> wrote: >>>> >>>> Hmm, this sounds wrong (C++ should be (a lot) faster on such micro benchmarks). >>>> >>>> You should exclude the executable startup time. >>>> >>>> In Pharo you should do [ .. ] timeToRun and something similar in C/C++. >>>> >>>>> On 17 Dec 2016, at 13:41, Dimitris Chloupis <kilon.al...@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.al...@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.