Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Dark Shikari

By: a guest on Sep 16th, 2008  |  syntax: C  |  size: 1.51 KB  |  views: 810  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4.  
  5. static inline uint64_t read_time(void)
  6. {
  7.     if(sizeof(long)==8)
  8.     {
  9.         uint64_t a, d;
  10.         asm volatile( "rdtsc\n\t" : "=a" (a), "=d" (d) );
  11.         return (d << 32) | (a & 0xffffffff);
  12.     } else {
  13.         uint64_t l;
  14.         asm volatile( "rdtsc\n\t" : "=A" (l) );
  15.         return l;
  16.     }
  17. }
  18.  
  19. #define NOP_CYCLES 65 // time measured by an empty timer on Core2
  20.  
  21. #define START_TIMER \
  22. uint64_t tend;\
  23. uint64_t tstart= read_time();
  24.  
  25. #define STOP_TIMER(id) {\
  26. tend= read_time();\
  27. {\
  28.     static uint64_t tsum=0;\
  29.     static int tcount=0;\
  30.     static int tskip_count=0;\
  31.     if(tskip_count<2)\
  32.         tskip_count++;\
  33.     else{\
  34.     if(tcount<2 || tend - tstart < 8*tsum/tcount){\
  35.         tsum+= tend - tstart;\
  36.         tcount++;\
  37.     }else\
  38.         tskip_count++;\
  39.     if(((tcount+tskip_count) & (tcount+tskip_count-1)) == 0)\
  40.         printf("%"PRIu64" dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount-NOP_CYCLES*10, id, tcount, tskip_count);\
  41. }}}
  42.  
  43. #define STOP_TIMER_SUM(id) {\
  44. tend= read_time();\
  45. {\
  46.     static uint64_t tsum=0;\
  47.     static uint64_t tother=0;\
  48.     static uint64_t tend0=0;\
  49.     static int tcount=0;\
  50.     tsum += tend - tstart;\
  51.     if(tcount)\
  52.         tother += tstart - tend0;\
  53.     tend0 = tend;\
  54.     tcount++;\
  55.     if((tcount & (tcount-1)) == 0 && tcount > 4)\
  56.         printf("%"PRIu64"/%"PRIu64" cycles %s, %d runs\n", tsum-NOP_CYCLES*tcount, tother+tsum-NOP_CYCLES*tcount, id, tcount);\
  57. }}