测量 C++ 中调用 system() 的执行时间
我在这里找到了一些测量执行时间的代码 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用 gettimeofday?
Have you considered using gettimeofday?
不幸的是,clock() 在 Linux 上只有一秒的分辨率(即使它返回以微秒为单位的时间)。
许多人使用 gettimeofday() 进行基准测试,但这测量的是经过的时间 - 不是该进程/线程使用的时间 - 所以并不理想。显然,如果您的系统或多或少处于空闲状态并且您的测试相当长,那么您可以对结果进行平均。通常问题不大,但仍然值得了解的是 gettimeofday() 返回的时间是非单调的 - 例如,当您的系统首次连接到 NTP 时间服务器时,它可能会稍微跳跃。
用于基准测试的最佳工具是clock_gettime(),使用最适合您的任务的选项。
注意,但并非所有选项在所有 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.
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
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.