RDTSC (read-time stamp counter) を使ったクロック計測

rdtsc命令を使う方法もあります。この場合でもできるだけ

 HANDLE hProcess = GetCurrentProcess();
 SetPriorityClass(hProcess , REALTIME_PRIORITY_CLASS);

などを使用して他プロセスの影響をなるべく排除した方が良い結果が得られます。

(もちろんそのままハングアップしてしまうようなプログラムを実行する場合
はNORMAL_PRIORITY_CLASSなどにしておきましょう(^_^;))

 #include 

 typedef __int64 LONGLONG;

 int main()
 {
   LONGLONG start = 0, end = 0;
 
   __asm {
     rdtsc
     mov dword ptr [start], eax
     mov dword ptr [start + 4], edx
   }
   
   for (int i = 0; i < 1000000; i++);
 
   __asm {
     rdtsc
     mov dword ptr [end], eax
     mov dword ptr [end + 4], edx
   }
 
   std::cout << "start : " << start << ", end : " << end << ", and diff : " << end - start << std::endl;
 }

Borland製コンパイラでは以下のコードを入れないとエラーになります。

 #if defined(__BORLANDC__)
 #pragma option -5 // or -6 cpuid命令を有効にする
 #endif