Compiler optimizations

Craig Black cblack at ara.com
Sun Apr 30 12:04:03 PDT 2006


> The second one is faster because you cheat.

Nope. Try this one. On my computer the floating point division is twice as
fast.  I believe this is due to the overhead on converting the divisor from
int to double before performing the division.

#include <stdio.h>
#include <conio.h>
#include <time.h>

//typedef int divtype;
typedef double divtype; // this one is faster

int main()
{
   int result = 0;

   clock_t start, finish;
   double  duration;

   start = clock();
   for(divtype div=1; div<10000; div++)
   {
      for(int i=0; i<10000; i++)
      {
        result += i / div;
      }
   }
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;

   printf("[%i] %2.1f seconds\n",result,duration);
}





More information about the Digitalmars-d mailing list