В процессорах Intel и AMD есть высокоскоростной счетчик. Windows API включает в себя вызовы функций для считывания значения этого счетчика, а также частоты счетчика, т. Е. Сколько раз в секунду он рассчитывает.
Вот пример того, как рассчитать время в микросекундах:
#include <iostream>
#include <windows.h>
int main()
{
__int64 ctr1 = 0, ctr2 = 0, freq = 0;
// Start timing the code.
if (QueryPerformanceCounter((LARGE_INTEGER *) &ctr1) != 0) {
// Do what ever you do, what ever you need to time...
//
//
//
// Finish timing the code.
QueryPerformanceCounter((LARGE_INTEGER *) &ctr2);
QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
// Print the time spent in microseconds to the console.
std::cout << ((ctr2 - ctr1) * 1.0 / freq) << std::endl;
}
}