[英]Measuring amount of CPU time taken by a piece of code, in C on Unix/Linux
[英]Measuring time taken by an exec()-ed process on linux
我正在使用times()函数测量值,但不确定我的方法是否正确。 请看看和建议
struct tms tms_start, tms_end;
if (!(pid=fork()))
{
//some necessary operations here
times(&tms_start);
execl(...);
}
else if (pid)
{
//in parent
int status;
wait(&status);
times(&tms_end);
if (WIFEXITED(status))
{
if(WEXITSTATUS(status)==0)
{
clock_t real = tms_end.tms_cstime - tms_start.tms_stime
float running_time = real/(double)sysconf(_SC_CLK_TK);
}
}
}
您需要在调用fork()
之前先调用times(&tms_start)
fork()
。 在上面的代码中, tms_start
变量在父级中未初始化,因为父级从不调用times(&tms_start)
。
struct tms tms_start, tms_end;
times(&tms_start); // <-- here
if (!(pid=fork()))
{
//some necessary operations here
execl(...);
}
else if (pid)
{
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.