c++ - 如何在没有 boost::timer 的情况下以毫秒为单位计时

标签 c++ boost timer std timing

我正在使用不包含 boost::timer 的 boost 1.46,还有什么其他方法可以为我的函数计时。

我目前正在这样做:

time_t now = time(0);
<some stuff>
time_t after = time(0);

cout << after - now << endl; 

但它只是在几秒钟内给出答案,所以如果函数花费 < 1s 它会显示 0。

谢谢

最佳答案

在 linux 或 Windows 中:

#include <ctime>
#include <iostream>

int
main(int, const char**)
{
     std::clock_t    start;

     start = std::clock();
     // your test
     std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
     return 0;
}

祝你好运;)

关于c++ - 如何在没有 boost::timer 的情况下以毫秒为单位计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15092504/

相关文章:

c++ - 编译器或定义行为的可能优化

c++ - 使用 conan 仅安装 boost 的一部分

c++ - 来自模板化迭代器的 boost::iterator_range

c++ - boost::unique_lock 和 boost::upgrade_lock 之间的区别?

Javascript:如何更新我的事件监听器以仅在用户停止输入时进行更改?

c++ - 跳过第 3 方库中的 #define

c++ - 虚赋值运算符重载——如何选择正确的重载函数?

c++ - 从 C++ 调用 MSWord 的最佳方法是什么

Javascript - 测量 FPS 并将其与事件同步

python - 将 threading.Timer 与 asyncio 一起使用