precise time measurement(精确的时间测量)
问题描述
我在 C++ 中使用 time.h 来测量函数的计时.
I'm using time.h in C++ to measure the timing of a function.
clock_t t = clock();
someFunction();
printf("
Time taken: %.4fs
", (float)(clock() - t)/CLOCKS_PER_SEC);
但是,我总是将时间设为 0.0000.clock() 和 t 在单独打印时具有相同的值.我想知道是否有办法在 C++ 中精确测量时间(可能以纳秒为单位).我使用的是 VS2010.
however, I'm always getting the time taken as 0.0000. clock() and t when printed separately, have the same value. I would like to know if there is way to measure the time precisely (maybe in the order of nanoseconds) in C++ . I'm using VS2010.
推荐答案
我通常使用 QueryPerformanceCounter
函数.
I usually use the QueryPerformanceCounter
function.
示例:
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1, t2; // ticks
double elapsedTime;
// get ticks per second
QueryPerformanceFrequency(&frequency);
// start timer
QueryPerformanceCounter(&t1);
// do something
...
// stop timer
QueryPerformanceCounter(&t2);
// compute and print the elapsed time in millisec
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
这篇关于精确的时间测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:精确的时间测量


基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01