v 0. Pasted by Anonymous as cpp at 2009-07-10 23:56:30 MSK and set expiration to never.
v 1. Edited by Anonymous as cpp at 2009-07-10 23:57:22 MSK and set expiration to never.

Paste will expire never.

  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int wmain(void)
  5. {
  6.     DWORD           nFirstTime;
  7.     DWORD           nLastTime;
  8.     double          fResolutionQPC;
  9.     LARGE_INTEGER   nFrequencyQPC;
  10.     LARGE_INTEGER   nFirstQPC;
  11.     LARGE_INTEGER   nLastQPC;
  12.     LARGE_INTEGER   nDummyQPC;
  13.     __int64         nFirstTSC;
  14.     __int64         nLastTSC;
  15.  
  16.     printf("-- Timer measurement --\n\t\tResolution\t\tStep\t\t\tCall cost\n");
  17.     for(int i = 0; i <= 2; ++i)
  18.     {
  19.         nFirstTime = ::GetTickCount();
  20.         do
  21.         {
  22.             nLastTime = ::GetTickCount();
  23.         }
  24.         while(nLastTime == nFirstTime);
  25.         nFirstTSC = __rdtsc();
  26.         nLastTime = ::GetTickCount();
  27.         nLastTSC = __rdtsc();
  28.         printf("GetTickCount():\t1000Hz (1ms)\t\t%-uHz (%-ums)\t\t%llu ticks\n",
  29.             1000 / (nLastTime - nFirstTime),
  30.             nLastTime - nFirstTime,
  31.             nLastTSC - nFirstTSC);
  32.     }
  33.     for(int i = 0; i <= 2; ++i)
  34.     {
  35.         nFirstTime = ::timeGetTime();
  36.         do
  37.         {
  38.             nLastTime = ::timeGetTime();
  39.         }
  40.         while(nLastTime == nFirstTime);
  41.         nFirstTSC = __rdtsc();
  42.         nLastTime = ::timeGetTime();
  43.         nLastTSC = __rdtsc();
  44.         printf("timeGetTime():\t1000Hz (1ms)\t\t%-uHz (%-ums)\t\t%llu ticks\n",
  45.             1000 / (nLastTime - nFirstTime),
  46.             nLastTime - nFirstTime,
  47.             nLastTSC - nFirstTSC);
  48.     }
  49.  
  50.     printf("timeBeginPeriod(1)\n");
  51.     ::timeBeginPeriod(1);
  52.     for(int i = 0; i <= 2; ++i)
  53.     {
  54.         nFirstTime = ::GetTickCount();
  55.         do
  56.         {
  57.             nLastTime = ::GetTickCount();
  58.         }
  59.         while(nLastTime == nFirstTime);
  60.         nFirstTSC = __rdtsc();
  61.         nLastTime = ::GetTickCount();
  62.         nLastTSC = __rdtsc();
  63.         printf("GetTickCount():\t1000Hz (1ms)\t\t%-uHz (%-ums)\t\t%llu ticks\n",
  64.             1000 / (nLastTime - nFirstTime),
  65.             nLastTime - nFirstTime,
  66.             nLastTSC - nFirstTSC);
  67.     }
  68.     for(int i = 0; i <= 2; ++i)
  69.     {
  70.         nFirstTime = ::timeGetTime();
  71.         do
  72.         {
  73.             nLastTime = ::timeGetTime();
  74.         }
  75.         while(nLastTime == nFirstTime);
  76.         nFirstTSC = __rdtsc();
  77.         nLastTime = ::timeGetTime();
  78.         nLastTSC = __rdtsc();
  79.         printf("timeGetTime():\t1000Hz (1ms)\t\t%-uHz (%-ums)\t\t%llu ticks\n",
  80.             1000 / (nLastTime - nFirstTime),
  81.             nLastTime - nFirstTime,
  82.             nLastTSC - nFirstTSC);
  83.     }
  84.     ::timeEndPeriod(1);
  85.     printf("timeEndPeriod(1)\n");
  86.  
  87.     ::QueryPerformanceFrequency(&nFrequencyQPC);
  88.     fResolutionQPC = 1000000. / nFrequencyQPC.QuadPart;
  89.     for(int i = 0; i <= 2; ++i)
  90.     {
  91.         ::QueryPerformanceCounter(&nFirstQPC);
  92.         ::QueryPerformanceCounter(&nLastQPC);
  93.         nFirstTSC = __rdtsc();
  94.         ::QueryPerformanceCounter(&nDummyQPC);
  95.         nLastTSC = __rdtsc();
  96.         double fStepResolution = (nLastQPC.QuadPart - nFirstQPC.QuadPart) * fResolutionQPC;
  97.         printf("QPC:\t\t%-lluHz (%-.3fusec)\t%-lluHz (%-.3fusec)\t%llu ticks\n",
  98.             nFrequencyQPC.QuadPart,
  99.             fResolutionQPC,
  100.             nFrequencyQPC.QuadPart / (nLastQPC.QuadPart - nFirstQPC.QuadPart),
  101.             fStepResolution,
  102.             nLastTSC - nFirstTSC);
  103.     }
  104.     return 0;
  105. }