in reply to Re^5: code optimization
in thread code optimization
I did a similar thing using C:
#include <stdio.h> #define ITERS 1000000000ul int main( int argc, char **argv ) { __int64 start; int i; double d; getch(); start = GetTickCount64(); if( argc > 1 ) { printf( "%u integer divisions: ", ITERS ); start = GetTickCount64(); for( i = 1; i < ITERS; i++ ) d = 1 / i; printf( "Took %I64d ticks\n", GetTickCount64() - start ); } else { printf( "%u integer multiplications: ", ITERS ); start = GetTickCount64(); for( i = 1; i < ITERS; i++ ) d = 1 * i; printf( "Took %I64d ticks\n", GetTickCount64() - start ); } }
And on my 64-bit processor, for 32-bit ints I got:
C:\test>muldiv-b 1 1000000000 integer divisions: Took 3432 ticks C:\test>muldiv-b 1000000000 integer multiplications: Took 2917 ticks
The numbers vary ~+-30 ticks for individual runs, but division is always ~10% slower than multiplication. I put this down the subsequent promotion of the result to a double rather than the opcode itself.
Conversely, if I use 64-bit ints division is almost 7X slower than multiplication:
C:\test>muldiv-b 1000000000 integer multiplications: Took 3011 ticks C:\test>muldiv-b 1 1000000000 integer divisions: Took 20764 ticks
This is think is due to the fact that two 64-bit registers are involved in the result.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: code optimization
by spx2 (Deacon) on Nov 04, 2011 at 15:27 UTC | |
by BrowserUk (Patriarch) on Nov 04, 2011 at 16:10 UTC |