在C ++中测量对system()的调用的执行时间 [英] Measuring execution time of a call to system() in C++
问题描述
我在这里找到了一些测量执行时间的代码
http: //www.dreamincode.net/forums/index.php?showtopic=24685
但是,它似乎不适用于对system()的调用, 。我想这是因为执行跳出当前进程。
clock_t begin = clock();
system(something);
clock_t end = clock();
cout<<执行时间:<<< 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秒...还有另一种方法?
此外,只是为了添加,执行时间是以小时为单位。所以准确性不是真正的问题。
谢谢!
http://dell5.ma.utexas.edu/cgi-bin/man-cgi?gettimeofday+2\"> 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;
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!
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;
这篇关于在C ++中测量对system()的调用的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!