getrusage 在 ru_utime.tv_usec 和 ru_utime.tv_sec 中返回零 [英] getrusage returning zeros in ru_utime.tv_usec and ru_utime.tv_sec
问题描述
对于以下代码,getrusage 在 ru_utime.tv_usec
和 ru_utime.tv_sec
中返回零.
For the following code getrusage returning zeros in ru_utime.tv_usec
and ru_utime.tv_sec
.
代码:
#include "stdlib.h"
#include "stdio.h"
#include "sys/time.h"
#include "sys/resource.h"
int getr_return, who = RUSAGE_SELF;
struct rusage usage;
main()
{
getr_return = getrusage(who, &usage);
printf(" getr_return = %d\n", getr_return);
printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec);
printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec);
Some_Mips_consuming_code().
getr_return = getrusage(who, &usage);
printf(" getr_return = %d\n", getr_return);
printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec);
printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec);
exit;
}
输出:
getr_return = 0
time taken in seconds = 0.0000000000000000000000000000000000000000000000000000000000000
time taken in seconds = 0.0000000000000000000000000000000000000000000000000000000000000
getr_return = 0
time taken in seconds = 0.0000000000000000000000000000000000000000000000000000000000000
time taken in seconds = 0.0000000000000000000000000000000000000000000000000000000000000
<小时>
在Linux version 2.6.18-308
在 ARM 板上运行可执行文件,它是 Linux 版本 3.8.1-2.0
Ran executable on ARM board and it's Linux version 3.8.1-2.0
推荐答案
时间测量在精度、分辨率和准确度方面受到限制.阅读time(7).不要期望重要的计算量小于例如半秒.
Time measurement is limited in precision, resolution and accuracy. Read time(7). Don't expect significant measures for computation less than e.g. half a second.
通常,CPU 时间测量是通过计算 jiffies 或定时器中断(与 HZ
如果你的内核中有一个).
Often, CPU time measurement is done by counting jiffies or timer interrupts (related to HZ
if you have one in your kernel).
因为你在开头调用了 getrusage(2)在您的 main
之前,并没有发生太多计算(基本上,只有在 crt0.o
).所以你应该期望它接近于零.
Since you call getrusage(2) at the beginning of your main
, not much computing has happened before it (basically, only pre-main
initialization in e.g. crt0.o
). So you should expect it to be near zero.
您可以尝试将 clock_gettime(2) 与 CLOCK_REALTIME
或 CLOCK_PROCESS_CPUTIME_ID
.
You might try using clock_gettime(2) with CLOCK_REALTIME
or CLOCK_PROCESS_CPUTIME_ID
.
这篇关于getrusage 在 ru_utime.tv_usec 和 ru_utime.tv_sec 中返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!