[Stupid Benchmarks] C++ vs Pharo

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

[Stupid Benchmarks] C++ vs Pharo

kilon.alios
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. 

Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

kilon.alios
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 <[hidden email]> 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. 

Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Sven Van Caekenberghe-2
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 <[hidden email]> 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 <[hidden email]> 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.
>


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Michal Balda
In reply to this post by kilon.alios

The conditions are unfair: the C++ version uses double, whereas the Pharo version uses Integers. Change the C++ version to use int and then report the results :-).


Michal



On 17.12.2016 13:16, Dimitris Chloupis 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. 


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Sven Van Caekenberghe-2
In reply to this post by Sven Van Caekenberghe-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Henrik Nergaard-2
In reply to this post by kilon.alios

You are comparing two different types here, double vs a 32 bit integer, and program starting time should be excluded.

Something like:

 

In playground:

Smalltalk garbageCollect.

[ 0 to: 16r0FFFFFFF do: [ :x | ] ] timeToRun.

“ try switch the 0 to 0.0 to have the iterations be on a float, leads to much slower time”

 

C++ (compile with -std=c++11)

 

#include <iostream>

#include <chrono>

 

int main(){

               

                using namespace std::chrono;

 

                auto start = high_resolution_clock::now();

                for(uint32_t x{0}; x <= 0x0FFFFFFF; ++x){ }            

                auto end = high_resolution_clock::now();

                auto ms = duration_cast<milliseconds>(end - start).count();

 

                std::cout

                                << "time used: "

                                << ms

                                << "ms"

                                << std::endl;

}

 

Best regards,

Henrik

 

 

From: Pharo-users [mailto:[hidden email]] On Behalf Of Dimitris Chloupis
Sent: Saturday, December 17, 2016 1:16 PM
To: Any question about pharo is welcome <[hidden email]>
Subject: [Pharo-users] [Stupid Benchmarks] C++ vs Pharo

 

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. 

 

Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Clément Béra
In reply to this post by kilon.alios
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 <[hidden email]> 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 <[hidden email]> 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. 


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Henrik Nergaard-2
In reply to this post by Sven Van Caekenberghe-2
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

Best regards,
Henrik

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of Sven Van Caekenberghe
Sent: Saturday, December 17, 2016 2:15 PM
To: Any question about pharo is welcome <[hidden email]>
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.
>>
>



Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Sven Van Caekenberghe-2

> On 17 Dec 2016, at 14:28, Henrik Nergaard <[hidden email]> 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.

> Best regards,
> Henrik
>
> -----Original Message-----
> From: Pharo-users [mailto:[hidden email]] On Behalf Of Sven Van Caekenberghe
> Sent: Saturday, December 17, 2016 2:15 PM
> To: Any question about pharo is welcome <[hidden email]>
> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.
>>>
>>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

kilon.alios
In reply to this post by Sven Van Caekenberghe-2
Yes I stand corrected, with unsigned ints and optimisations turned on the time for C++ for multiplication drops from 3.13 to 0.39 thats an almost 10 times speed up , with doubles and max optimisation on its 0.9

Yes I am using Pharo 32 bit. I think so, unless vmLatest now downloads Pharo 64 bit. 

So the conclusion is that C++ without the max optimisation on is almost as fast as Pharo.  

Still 10 times diffirence is not bad at all , on the other hand CPython3 on my iMac needs 118 seconds to finish the multiplication and for some strange reason the python 2.7 included with macos just hangs there forever. 

So Cpython at for me appears around 30-20 times slower... which is kinda weird too. But then Cpython has no JIT VM. 



 

 
Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Sven Van Caekenberghe-2
In reply to this post by Sven Van Caekenberghe-2

> On 17 Dec 2016, at 15:23, Sven Van Caekenberghe <[hidden email]> wrote:
>
>
>> On 17 Dec 2016, at 14:28, Henrik Nergaard <[hidden email]> 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


>> Best regards,
>> Henrik
>>
>> -----Original Message-----
>> From: Pharo-users [mailto:[hidden email]] On Behalf Of Sven Van Caekenberghe
>> Sent: Saturday, December 17, 2016 2:15 PM
>> To: Any question about pharo is welcome <[hidden email]>
>> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.
>>>>
>>>
>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

Sven Van Caekenberghe-2

> On 17 Dec 2016, at 15:52, Sven Van Caekenberghe <[hidden email]> wrote:
>
>>
>> On 17 Dec 2016, at 15:23, Sven Van Caekenberghe <[hidden email]> wrote:
>>
>>
>>> On 17 Dec 2016, at 14:28, Henrik Nergaard <[hidden email]> 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:[hidden email]] On Behalf Of Sven Van Caekenberghe
>>> Sent: Saturday, December 17, 2016 2:15 PM
>>> To: Any question about pharo is welcome <[hidden email]>
>>> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.


Reply | Threaded
Open this post in threaded view
|

Re: [Stupid Benchmarks] C++ vs Pharo

kilon.alios
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 <[hidden email]> wrote:

> On 17 Dec 2016, at 15:52, Sven Van Caekenberghe <[hidden email]> wrote:
>
>>
>> On 17 Dec 2016, at 15:23, Sven Van Caekenberghe <[hidden email]> wrote:
>>
>>
>>> On 17 Dec 2016, at 14:28, Henrik Nergaard <[hidden email]> 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:[hidden email]] On Behalf Of Sven Van Caekenberghe
>>> Sent: Saturday, December 17, 2016 2:15 PM
>>> To: Any question about pharo is welcome <[hidden email]>
>>> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.