测量 C++ 中调用 system() 的执行时间

发布于 2024-08-26 11:32:38 字数 723 浏览 4 评论 0原文

我在这里找到了一些测量执行时间的代码 http://www.dreamincode.net/forums/index.php?showtopic= 24685

但是,它似乎不适用于对 system() 的调用。我想这是因为执行跳出了当前进程。

clock_t begin=clock();

system(something);

clock_t end=clock();
cout<<"Execution time: "<<diffclock(end,begin)<<" s."<<endl;

然后

double diffclock(clock_t clock1,clock_t clock2)
{
    double diffticks=clock1-clock2;
    double diffms=(diffticks)/(CLOCKS_PER_SEC);
    return diffms;
}

然而这总是返回 0 秒...还有另一种方法可以工作吗?

另外,这是在 Linux 中。

编辑:此外,只是补充一下,执行时间以小时为单位。所以准确度并不是真正的问题。

谢谢!

I have found some code on measuring execution time here
http://www.dreamincode.net/forums/index.php?showtopic=24685

However, it does not seem to work for calls to system(). I imagine this is because the execution jumps out of the current process.

clock_t begin=clock();

system(something);

clock_t end=clock();
cout<<"Execution time: "<<diffclock(end,begin)<<" s."<<endl;

Then

double diffclock(clock_t clock1,clock_t clock2)
{
    double diffticks=clock1-clock2;
    double diffms=(diffticks)/(CLOCKS_PER_SEC);
    return diffms;
}

However this always returns 0 seconds... Is there another method that will work?

Also, this is in Linux.

Edit: Also, just to add, the execution time is in the order of hours. So accuracy is not really an issue.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

独自←快乐 2024-09-02 11:32:38

您是否考虑过使用 gettimeofday

struct timeval tv;
struct timeval start_tv;

gettimeofday(&start_tv, NULL);

system(something);

double elapsed = 0.0;

gettimeofday(&tv, NULL);
elapsed = (tv.tv_sec - start_tv.tv_sec) +
  (tv.tv_usec - start_tv.tv_usec) / 1000000.0;

Have you considered using gettimeofday?

struct timeval tv;
struct timeval start_tv;

gettimeofday(&start_tv, NULL);

system(something);

double elapsed = 0.0;

gettimeofday(&tv, NULL);
elapsed = (tv.tv_sec - start_tv.tv_sec) +
  (tv.tv_usec - start_tv.tv_usec) / 1000000.0;
甜妞爱困 2024-09-02 11:32:38

不幸的是,clock() 在 Linux 上只有一秒的分辨率(即使它返回以微秒为单位的时间)。

许多人使用 gettimeofday() 进行基准测试,但这测量的是经过的时间 - 不是该进程/线程使用的时间 - 所以并不理想。显然,如果您的系统或多或少处于空闲状态并且您的测试相当长,那么您可以对结果进行平均。通常问题不大,但仍然值得了解的是 gettimeofday() 返回的时间是非单调的 - 例如,当您的系统首次连接到 NTP 时间服务器时,它可能会稍微跳跃。

用于基准测试的最佳工具是clock_gettime(),使用最适合您的任务的选项。

  • CLOCK_THREAD_CPUTIME_ID - 线程特定的 CPU 时间时钟。
  • CLOCK_PROCESS_CPUTIME_ID - 来自 CPU 的高分辨率每进程计时器。
  • CLOCK_MONOTONIC - 表示自某个未指定的起点以来的单调时间。
  • CLOCK_REALTIME - 系统范围的实时时钟。

注意,但并非所有选项在所有 Linux 平台上都受支持 - 除了与 gettimeofday() 等效的clock_gettime(CLOCK_REALTIME) 之外。

有用的链接:使用clock_gettime分析代码

Unfortunately clock() only has one second resolution on Linux (even though it returns the time in units of microseconds).

Many people use gettimeofday() for benchmarking, but that measures elapsed time - not time used by this process/thread - so isn't ideal. Obviously if your system is more or less idle and your tests are quite long then you can average the results. Normally less of a problem but still worth knowing about is that the time returned by gettimeofday() is non-monatonic - it can jump around a bit e.g. when your system first connects to an NTP time server.

The best thing to use for benchmarking is clock_gettime() with whichever option is most suitable for your task.

  • CLOCK_THREAD_CPUTIME_ID - Thread-specific CPU-time clock.
  • CLOCK_PROCESS_CPUTIME_ID - High-resolution per-process timer from the CPU.
  • CLOCK_MONOTONIC - Represents monotonic time since some unspecified starting point.
  • CLOCK_REALTIME - System-wide realtime clock.

NOTE though, that not all options are supported on all Linux platforms - except clock_gettime(CLOCK_REALTIME) which is equivalent to gettimeofday().

Useful link: Profiling Code Using clock_gettime

农村范ル 2024-09-02 11:32:38

Tuomas Pelkonen 已经提出了 gettimeofday 方法,该方法允许获取精确到微秒的时间。

在他的例子中,他继续转换为双精度。我个人已将 timeval 结构包装到我自己的类中,该类将计数保留为秒和微秒整数,并正确处理加法和减法操作。

我更喜欢保留整数(具有精确的数学),而不是尽可能地处理浮点数及其所有问题。

Tuomas Pelkonen already presented the gettimeofday method that allows to get times with a resolution to the microsecond.

In his example he goes on to convert to double. I personally have wrapped the timeval struct into a class of my own that keep the counts into seconds and microseconds as integers and handle the add and minus operations correctly.

I prefer to keep integers (with exact maths) rather than get to floating points numbers and all their woes when I can.

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