Compiler optimizations

Craig Black cblack at ara.com
Sun Apr 30 11:35:20 PDT 2006


Yours used a constant divisor which allowed the compiler to optimize it
somehow.  That's one of the things that I'm trying to figure out.  How does
the compiler optimize integer division so well when the divisor is constant?
Anyway, try this one.

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

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

int Division(divtype div)
{
  int result = 0;
  for(int i=0; i<10000; i++)
  {
     result += i / div;
  }
  return result;
}

int main()
{
   int result = 0;
   int div = 10; //(or double) !!!

   clock_t start, finish;
   double  duration;

   start = clock();
   for(divtype div=1; div<10000; div++)
   {
     result = Division(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