Compiler optimizations
Walter Bright
newshound at digitalmars.com
Wed May 3 16:18:09 PDT 2006
On my machine, the int version is 1.7 seconds, the double is 3.2. I am
unable to reproduce your results.
Craig Black wrote:
>> 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