1.计时5种方式
共有五种计算时间的方式分别如下:
//需要包含 “<Windows.h>” LARGE_INTEGER a, a_, nFrequency; double timeAccuracy = 1000; QueryPerformanceFrequency(&nFrequency); QueryPerformanceCounter(&a); Sleep(period); QueryPerformanceCounter(&a_); printf("%.8f\n", timeAccuracy * ((double)a_.QuadPart - a.QuadPart) / nFrequency.QuadPart); double b = GetTickCount(); Sleep(period); double c = GetTickCount(); printf("%.8f\n", (c-b)); //需要包含"opencv2/core.hpp" double d = cvGetTickCount(); Sleep(period); double e = cvGetTickCount(); printf("%.8f\n", (e - d) / cvGetTickFrequency()/1000); //需要包含"<time.h>" long f = clock(); Sleep(period); long g = clock(); printf("%.8f\n", ((double)g - f) / CLOCKS_PER_SEC*1000); //需要包含"<chrono>,<numeric>,<iostream>" auto h = std::chrono::system_clock::now(); Sleep(period); auto i = std::chrono::system_clock::now(); std::chrono::duration<double> diff = i - h; printf("%.8f\n", diff.count()*1000);以上输出均为毫秒.
2.计时精确度测量
上述输出全部为毫秒,在win7 sp1和64位机器,VS2013,release下。
当period设定为1,当10次循环求平均,输出为:
0.91895976
1.60000000
1.60000000
1.00481249
1.00000000
当period设定为1,当25次循环求平均,输出为:
0.98673408
0.64000000
0.64000000
0.99715482
1.00000000
当period设定为1,当50次循环求平均,输出为:
0.99323125
0.94000000
0.94000000
0.99962580
1.00000000
从稳定性和精确性的角度来说:
方式5>方式4>方式1>方式3>方式2
3.建议计时方式
在C中,优先使用方式4,其次是方式1
在C++中,优先使用方式5,其次是4和1,不精确计量情况下,可以调用opencv,即使用方式3