我一直在使用Windows 中的GetTickCount(),但我读到它的性能/分辨率很差,我想问一下什么是获得时间的最佳方法。
我尝试使用<ctime>库,但它不会持续毫秒或微秒,我需要达到那个精度的东西。
谢谢!
我一直在使用Windows 中的GetTickCount(),但我读到它的性能/分辨率很差,我想问一下什么是获得时间的最佳方法。
我尝试使用<ctime>库,但它不会持续毫秒或微秒,我需要达到那个精度的东西。
谢谢!
如果使用 C++11,您可以使用std::chrono::high_resolution_clock
精度是一个实现细节,例如它应该QueryPerformanceCounter
在 Windows 上实现。
#include <chrono>
#include <iostream>
int main(int argc, char *argv[])
{
auto t1 = std::chrono::high_resolution_clock::now();
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "It took " << std::chrono::nanoseconds(t2 - t1).count() << " nanoseconds" << std::endl;
return 0;
}