This question already has answers here:
Timer function to provide time in nano seconds using C++

(16个回答)


7年前关闭。




C++中有没有办法获得纳秒精度?相当于Java中的纳秒计时器(System.nanotime)。

最佳答案

如果您使用的是Windows / Visual Studio(可能未实现std::chrono,至少在VS2010中未实现),则可以使用QueryPerformanceCounterQueryPerformanceFrequency在计时任务上获得最高的准确性:

LARGE_INTEGER freq, counter1, counter2;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&counter1);
task();
QueryPerformanceCounter(&counter2);
long double elapsed = (long double) (counter2.QuadPart - counter1.QuadPart) / (long double) freq.QuadPart;

关于c++ - C++中计时的纳秒精度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17567953/

10-10 04:23