Created
December 26, 2016 22:27
-
-
Save PinkPandaKatie/5e2709404072266bc11e76095e81d2ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/time.h> | |
/* compile with gcc -m32 -O2 -o looptest */ | |
unsigned int looptest1() { | |
unsigned int rv = 0; | |
asm volatile ( | |
"mov $0x80000000, %%ecx\n" | |
"1:\n" | |
" add %%ecx, %0\n" | |
" loop 1b\n" | |
: "+r" (rv) | |
: | |
: "ecx", "cc" | |
); | |
return rv; | |
} | |
unsigned int looptest2() { | |
unsigned int rv = 0; | |
asm volatile ( | |
"mov $0x80000000, %%ecx\n" | |
"1:\n" | |
" add %%ecx, %0\n" | |
" dec %%ecx\n" | |
" jnz 1b\n" | |
: "+r" (rv) | |
: | |
: "ecx", "cc" | |
); | |
return rv; | |
} | |
unsigned int looptest3() { | |
unsigned int rv = 0; | |
asm volatile ( | |
"mov $0x80000000, %%ecx\n" | |
"1:\n" | |
" add %%ecx, %0\n" | |
" sub $1, %%ecx\n" | |
" cmp $0, %%ecx\n" | |
" jnz 1b\n" | |
: "+r" (rv) | |
: | |
: "ecx", "cc" | |
); | |
return rv; | |
} | |
long long timefunc(unsigned int (*func)()) { | |
struct timeval tv_start, tv_end; | |
gettimeofday(&tv_start, NULL); | |
func(); | |
gettimeofday(&tv_end, NULL); | |
tv_end.tv_sec -= tv_start.tv_sec; | |
tv_end.tv_usec -= tv_start.tv_usec; | |
return ((long long)tv_end.tv_sec * 1000000LL) + (long long)tv_end.tv_usec; | |
} | |
int main(int argc, char** argv) { | |
int i, j; | |
long long times[12]; | |
for (i = 0; i < 4; i++) { | |
/* Unroll the loop a few times so that any effect printf might | |
* have (branch prediction, trace cache, etc) are minimized */ | |
times[0] = timefunc(looptest1); | |
times[1] = timefunc(looptest2); | |
times[2] = timefunc(looptest3); | |
times[3] = timefunc(looptest1); | |
times[4] = timefunc(looptest2); | |
times[5] = timefunc(looptest3); | |
times[6] = timefunc(looptest1); | |
times[7] = timefunc(looptest2); | |
times[8] = timefunc(looptest3); | |
times[9] = timefunc(looptest1); | |
times[10] = timefunc(looptest2); | |
times[11] = timefunc(looptest3); | |
for (j = 0; j < 12; j += 3) { | |
/* Print microseconds in CSV-compatible format */ | |
printf("%lld,%lld,%lld\n", times[j + 0], times[j + 1], times[j + 2]); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment