getrusage在ru_utime.tv_usec和ru_utime.tv_sec中返回零 [英] getrusage returning zeros in ru_utime.tv_usec and ru_utime.tv_sec

查看:282
本文介绍了getrusage在ru_utime.tv_usec和ru_utime.tv_sec中返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关以下代码的getrusage返回零在 ru_utime.tv_usec ru_utime.tv_sec



代码:

  #includestdlib.h
#include stdio.h
#includesys / time.h
#includesys / resource.h

int getr_return,who = RUSAGE_SELF;
struct rusage用法;

main()
{
getr_return = getrusage(who,& usage);
printf(getr_return =%d \\\
,getr_return);

printf(以秒计的时间=%.61f\\\
,usage.ru_utime.tv_sec);
printf(以秒计的时间=%.61f\\\
,usage.ru_utime.tv_usec);


Some_Mips_consuming_code()。


getr_return = getrusage(who,& usage);
printf(getr_return =%d \\\
,getr_return);

printf(以秒计的时间=%.61f\\\
,usage.ru_utime.tv_sec);
printf(以秒计的时间=%.61f\\\
,usage.ru_utime.tv_usec);

出口;
}

输出:

在秒取 getr_return = 0 
时间=以秒为采取0.0000000000000000000000000000000000000000000000000000000000000
时间采取秒= 0.0000000000000000000000000000000000000000000000000000000000000 = 0.0000000000000000000000000000000000000000000000000000000000000
getr_return = 0
时间以秒计算的
时间= 0.0000000000000000000000000000000000000000000000000000000000000






Linux版本2.6.18-308上编译代码



在ARM板上运行可执行文件,它的 Linux版本3.8.1-2.0

解决方案

时间测量精度,分辨率和精度有限。阅读时间(7)。不要指望计算的重要措施少于例如半秒钟。

通常,CPU时间测量是通过计算 jiffies 或定时器中断(如果您的内核中有一个与 HZ 有关)。



<由于您在开始时致电 getrusage(2),你的 main ,在它之前没有发生太多的计算(基本上,只有在 crt0.o )。所以你应该预计它接近零。



你可以尝试使用 clock_gettime(2),提供 CLOCK_REALTIME CLOCK_PROCESS_CPUTIME_ID


For the following code getrusage returning zeros in ru_utime.tv_usec and ru_utime.tv_sec.

Code:

#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;
 } 

output:

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


Compiled code on Linux version 2.6.18-308

Ran executable on ARM board and it's Linux version 3.8.1-2.0

解决方案

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.

Often, CPU time measurement is done by counting jiffies or timer interrupts (related to HZ if you have one in your kernel).

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆