使用 C++ 计算执行时间,而不是使用
我需要计算 C++ 程序的执行时间。我目前正在使用 C 风格头
和以下代码:
clock_t tStart = clock();
...
printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
是否有可以用来计算执行时间的 C++ 头?请注意,这需要跨平台兼容。
I need to calculate the execution time of a C++ program. I am currently using the c-style head, <time.h>
with the following code:
clock_t tStart = clock();
...
printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
Is there a c++ header I can use to calculate execution time? Please note this needs to be cross-platform compatible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 C 标头的 C++ 版本。在开头添加“c”,并删除“.h”,
因此您需要
其余部分保持不变,因为您可以使用 C 方法。
You can use the C++ versions of C headers. Add 'c' at the beginning, and remove '.h'
So you need
The rest stays the same, as you can just use the C approach.
时间和时钟功能是因平台而异的功能之一。
我喜欢使用 Boost 的计时器库来完成您想要做的事情: http://www.boost.org/doc/libs/1_43_0/libs/timer/timer.htm,因为它跨平台运行良好,并且使用起来非常简单。
The time and clock functions are one of those things that vary from platform to platform.
I like to use Boost's timer library for what you are trying to do: http://www.boost.org/doc/libs/1_43_0/libs/timer/timer.htm as it works well cross platform, and is quite straightforward to use.