使用 C++ 计算执行时间,而不是使用

发布于 2024-09-08 08:18:03 字数 253 浏览 0 评论 0原文

我需要计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的他你的她 2024-09-15 08:18:03

您可以使用 C 标头的 C++ 版本。在开头添加“c”,并删除“.h”,

因此您需要

#include <ctime>

其余部分保持不变,因为您可以使用 C 方法。

You can use the C++ versions of C headers. Add 'c' at the beginning, and remove '.h'

So you need

#include <ctime>

The rest stays the same, as you can just use the C approach.

终难遇 2024-09-15 08:18:03

时间和时钟功能是因平台而异的功能之一。

我喜欢使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文