为什么tv_sec和tv_usec会给getrusage()带来意想不到的结果?

时间:2016-06-14 09:36:24

标签: getrusage

在这部分代码中,我试图使用getrusage来测量时间:

    getrusage(RUSAGE_SELF, &usage2);
    start2 = usage2.ru_stime;

    int counter;
    for (counter = 0; counter<10000; counter++) {

        int j;
        int found_elements = 0;
        for (j = b; j < c; j++) {
            Node *current = NULL;
            HASH_FIND_INT(mainhash,&j,current);
            if (current) {
                found_elements++;
            }
        }
    }

    getrusage(RUSAGE_SELF, &usage2);
    end2 = usage2.ru_stime;

    printf("%.8f", (double)((end2.tv_sec - start2.tv_sec) + (end2.tv_usec - start2.tv_usec)));

用这些:

    struct rusage usage1, usage2;
    struct timeval start1, end1, start2, end2;

但它在终端上给了我这个:

    112001.00000000

我不知道。我需要以5.54676686(秒)的格式经过的时间。我怎样才能正确地得到它呢?

0 个答案:

没有答案