Compiler optimizations

James Pelcis jpelcis at gmail.com
Sun Apr 30 10:16:37 PDT 2006


This doesn't really surprise me.  If I am reading it correctly, the
major difference comes from only doing the division once.  If you did
the division every time, the results might not be the same.

Craig Black wrote:
> These two functions show the difference that I'm talking about.  When
> benchmarking, pass in the same values for the divisor (pick a value between
> 2 and 30), and pass in a large enough value for total so that it will take
> some time for the CPU to finish.  Suprisingly, the second one is faster.
> 
> -Craig
> 
> int divTest1(int divisor, int total)
> {
>   int sum = 0;
>   for(int i = 0; i < total; i)
>   {
>     int quotient = i / divisor;
>     sum = quotient;
>   }
> }
> 
> int divTest2(int divisor, int total)
> {
>   int sum = 0;
>   double idiv = 1.0 / divisor;
>   for(int i = 0; i < total; i)
>   {
>     int quotient = i * idiv;
>     sum = quotient;
>   }
> }



More information about the Digitalmars-d mailing list