Created
March 25, 2013 09:46
-
-
Save zyxar/5236038 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <assert.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#if defined(__i386__) | |
static __inline__ unsigned long long rdtsc(void) | |
{ | |
unsigned long long int x; | |
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x)); | |
return x; | |
} | |
#elif defined(__x86_64__) | |
static __inline__ unsigned long long rdtsc(void) | |
{ | |
unsigned hi, lo; | |
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); | |
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); | |
} | |
#elif defined(__powerpc__) | |
static __inline__ unsigned long long rdtsc(void) | |
{ | |
unsigned long long int result=0; | |
unsigned long int upper, lower,tmp; | |
__asm__ volatile( | |
"0: \n" | |
"\tmftbu %0 \n" | |
"\tmftb %1 \n" | |
"\tmftbu %2 \n" | |
"\tcmpw %2,%0 \n" | |
"\tbne 0b \n" | |
: "=r"(upper),"=r"(lower),"=r"(tmp) | |
); | |
result = upper; | |
result = result<<32; | |
result = result|lower; | |
return(result); | |
} | |
#endif | |
#define N (1024*64) | |
int main(int argc, char* argv[]) | |
{ | |
unsigned long long a,b; | |
unsigned long long min,max; | |
char* p; | |
int i; | |
p = (char*)malloc(N); | |
assert( p!=(char*)0 ); | |
max = 0; | |
min = UINT64_MAX; | |
for(i=0; i<N; i++ ) { | |
a = rdtsc(); | |
p[i] = 0; | |
b = rdtsc() - a; | |
//if(i%(1024) == 0) | |
printf("%d\t%llu\n", i, b); | |
if( b > max ) max = b; | |
else if( b < min ) min = b; | |
} | |
printf("min=%llu\n", min); | |
printf("max=%llu\n", max); | |
free(p); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment