public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
C:\> cl -O2 badif.c
C:\> badif
(i & 0x80000000) == 0 time: 0.674000
(i & 0xFFFFFFFF) == 0 time: 0.677000
(i & 0x00000001) == 0 time: 0.677000
(i & 0x00000003) == 0 time: 0.674000
(i & 0x00000002) == 0 time: 0.691000
(i & 0x00000008) == 0 time: 1.051000
(i & 0x00000010) == 0 time: 1.232000
 
Target: i686-pc-cygwin
gcc version 4.3.4 20090804 (release) 1 (GCC)
c:\>sh -c "gcc -O9 badif.c -o badif-gcc.exe"
c:\>badif-gcc
(i & 0x80000000) == 0 time: 0.000000
(i & 0xFFFFFFFF) == 0 time: 0.702000
(i & 0x00000001) == 0 time: 1.060000
(i & 0x00000003) == 0 time: 1.046000
(i & 0x00000002) == 0 time: 1.060000
(i & 0x00000008) == 0 time: 1.061000
(i & 0x00000010) == 0 time: 1.030000
*/
 
#include <time.h>
#include <stdio.h>
 
#define F(v) static int F##v() { int s=0,i; for(i=0; i<1073741824; i++) if(0==(i&v)) s++; return s; }
#define D(v) { #v, &F##v }
 
F(0x80000000)
F(0xFFFFFFFF)
F(0x00000001)
F(0x00000003)
F(0x00000002)
F(0x00000008)
F(0x00000010)
 
static struct { const char* name; int (*func) (); } f[]={
D(0x80000000),
D(0xFFFFFFFF),
D(0x00000001),
D(0x00000003),
D(0x00000002),
D(0x00000008),
D(0x00000010),
};
 
int timeit(int (*func)())
{
int x = clock();
func();
return clock() - x;
}
 
int main()
{
int i;
for( i=0; i<sizeof(f)/sizeof(f[0]); i++ )
{
printf( "(i & %s) == 0 time: %f\n", f[i].name, (double)timeit(f[i].func) / (double)CLOCKS_PER_SEC );
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.