v 0. Pasted by Anonymous as cpp at 2009-07-10 23:53:48 MSK and set expiration to never.

Paste will expire never.

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4.  
  5. typedef unsigned long long uint64;
  6.  
  7. inline uint64 rdtsc(void)
  8. {
  9.     uint64 nResult;
  10.     __asm__ __volatile__("rdtsc" : "=A" (nResult) ::);
  11.     return nResult;
  12. }
  13.  
  14. int main()
  15. {
  16.     uint64 nFirstTSC;
  17.     uint64 nLastTSC;
  18.     struct timeval tvFirst, tvLast, tvDummy;
  19.     struct timespec tsFirst, tsLast, tsResolution, tsDummy;
  20.    
  21.     printf("-- Timer measurement --\n\t\t Resolution\t\tStep\t\t\tCall cost\n");
  22.     for(int i = 0; i <= 2; ++i)
  23.     {
  24.         gettimeofday(&tvFirst, NULL);
  25.         do
  26.         {
  27.             gettimeofday(&tvLast, NULL);
  28.         }
  29.         while((tvLast.tv_sec == tvFirst.tv_sec) && (tvLast.tv_usec == tvFirst.tv_usec));
  30.         nFirstTSC = rdtsc();
  31.         gettimeofday(&tvDummy, NULL);
  32.         nLastTSC = rdtsc();
  33.  
  34.         uint64 nStep;
  35.         if((tvLast.tv_usec - tvFirst.tv_usec) >= 0)
  36.         {
  37.             nStep = 1000000 * (tvLast.tv_sec - tvFirst.tv_sec);
  38.             nStep += tvLast.tv_usec - tvFirst.tv_usec;
  39.         }
  40.         else
  41.         {
  42.             nStep = 1000000 * (tsLast.tv_sec - tsFirst.tv_sec - 1);
  43.             nStep += 1000000 + tsLast.tv_nsec - tsFirst.tv_nsec;
  44.         }
  45.         printf("gettimeofday():  1MHz (1usec)\t\t%-lluHz (%-lluusec)\t%llu ticks\n", 1000000 / nStep, nStep, nLastTSC - nFirstTSC);
  46.     }
  47.  
  48.     clock_getres(CLOCK_REALTIME, &tsResolution);
  49.     double fResolution = tsResolution.tv_sec * 1000000000 + tsResolution.tv_nsec;
  50.     uint64 nFrequency = 1000000000 / fResolution;
  51.     for(int i = 0; i <= 2; ++i)
  52.     {
  53.         clock_gettime(CLOCK_REALTIME, &tsFirst);
  54.         do
  55.         {
  56.             clock_gettime(CLOCK_REALTIME, &tsLast);
  57.         }
  58.         while((tsLast.tv_sec == tsFirst.tv_sec) && (tsLast.tv_nsec == tsFirst.tv_nsec));
  59.         nFirstTSC = rdtsc();
  60.         clock_gettime(CLOCK_REALTIME, &tsDummy);
  61.         nLastTSC = rdtsc();
  62.  
  63.         uint64 nStep;
  64.         if((tsLast.tv_nsec - tsFirst.tv_nsec) >= 0)
  65.         {
  66.             nStep = 1000000000 * (tsLast.tv_sec - tsFirst.tv_sec);
  67.             nStep += tsLast.tv_nsec - tsFirst.tv_nsec;
  68.         }
  69.         else
  70.         {
  71.             nStep = 1000000000 * (tsLast.tv_sec - tsFirst.tv_sec - 1);
  72.             nStep += 1000000000 + tsLast.tv_nsec - tsFirst.tv_nsec;
  73.         }
  74.         double fStepResolution = (double)nStep / 1000.;
  75.         printf("clock_gettime(): %-lluHz (%-.3fusec)\t%-lluHz (%.3fusec)\t%llu ticks\n", nFrequency, fResolution / 1000., 1000000000 / nStep, fStepResolution, nLastTSC - nFirstTSC);
  76.     }
  77.     return 0;
  78. }