real issue for me is idiotic optimization of compiler: no-if C-code
becomes if-fy Asm output. Such an irony all-around. XD
Evgeney, you are still far from good feel of how funnily modern
processors and compilers perform and how interesting it is to
measure their performance in a meaningful way. There are lot more
irony coming. I donate you another example:
#include <stdio.h>
#include <time.h>
/// @return - time difference between start and stop in milliseconds
int ms_elapsed( clock_t start, clock_t stop )
{
return (int)( 1000.0 * ( stop - start ) / CLOCKS_PER_SEC );
}
int const Billion = 1000000000;
/// & with numbers up to Billion gives 0, 0, 2, 2 repeating pattern
int const Pattern_0_0_2_2 = 0x40000002;
/// @return - half of Billion
int unpredictableIfs()
{
int sum = 0;
for ( int i = 0; i < Billion; ++i )
{
// true, true, false, false ...
if ( ( i & Pattern_0_0_2_2 ) == 0 )
{
++sum;
}
}
return sum;
}
/// @return - half of Billion
int noIfs()
{
int sum = 0;
for ( int i = 0; i < Billion; ++i )
{
// 1, 1, 0, 0 ...
sum += ( i & Pattern_0_0_2_2 ) == 0;
}
return sum;
}
int main()
{
clock_t volatile start;
clock_t volatile stop;
int volatile sum;
printf( "Following is a puzzle for Evgeney Knyazhev:\n" );
start = clock();
sum = unpredictableIfs();
stop = clock();
printf( "Unpredictable ifs took %d msec, answer was %d\n"
, ms_elapsed(start, stop), sum );
start = clock();
sum = unpredictableIfs();
stop = clock();
printf( "Unpredictable ifs took %d msec, answer was %d\n"
, ms_elapsed(start, stop), sum );
start = clock();
sum = noIfs();
stop = clock();
printf( "Same without ifs took %d msec; answer was %d\n"
, ms_elapsed(start, stop), sum );
start = clock();
sum = unpredictableIfs();
stop = clock();
printf( "Unpredictable ifs took %d msec, answer was %d\n"
, ms_elapsed(start, stop), sum );
}
Compiled with VS2008 and VS2010; /O2 optimizations, for 32 bit.
Ran on Intel Core 2, OS Windows XP 32 and Intel Core 5 with Windows 7 64.
Output:
Following is a puzzle for Evgeney Knyazhev:
Unpredictable ifs took 1360 msec, answer was 500000000
Unpredictable ifs took 1015 msec, answer was 500000000
Same without ifs took 1032 msec; answer was 500000000
Unpredictable ifs took 4812 msec, answer was 500000000
The values did deviate a bit here or there but the pattern was
like that. Enjoy!