#define _XOPEN_SOURCE 700 #include #include #include #include static inline unsigned rdtsc() { #if defined __i386__ || defined __x86_64__ unsigned x; __asm__ __volatile__ ( "rdtsc" : "=a"(x) : : "rdx" ); // __asm__ __volatile__ ( "cpuid ; rdtsc" : "=a"(x) // : : "rbx", "rcx", "rdx" ); return x; #else struct timespec ts; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); return ts.tv_nsec; #endif } char buf[32768+100]; int main() { unsigned a=0; unsigned i, j, t, tmin=-1; unsigned long long tmean=0; unsigned overhead = -1; size_t n; for (i=0; i<0+1*4096; i++) { t = rdtsc(); __asm__ __volatile__("nop"); t = rdtsc()-t; if (t < overhead) overhead = t; } //overhead = 0; for (n=2; n<32768; n+=(n<64 ? 2 : n<512 ? 32 : n)) { tmin = -1; tmean = 0; for (i=0; i<0+1*4096; i++) { __asm__ __volatile__ ("" : : : "memory"); t = rdtsc(); for (j=0; j<64; j++) { memset(buf, 0, n); __asm__ __volatile__ ("" : : : "memory"); } t = rdtsc()-t; __asm__ __volatile__ ("" : : : "memory"); if (t < tmin) tmin = t; tmean += t; } tmin -= overhead; tmean -= 4096*overhead; tmin /= 64; tmean /= 64; tmean /= 4096; printf("size %zu: min=%u, avg=%llu\n", n, tmin, tmean); } }