0

In this part of code, I am trying to use getrusage to measure time:

    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)));

with these:

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

But it gives me this on the terminal:

    112001.00000000

which i know not true. I need the elapsed time in a format like 5.54676686 (seconds). How can I get it like this properly?

  • Where is `end2` set/initialized. Your code fragment only shows the (unused?) `end1`? – Christian.K Jun 14 '16 at 09:49
  • oops, typo there. corrected now. still not solved though. – wackyburkay Jun 14 '16 at 09:51
  • You cannot simply add micro-seconds `tv_usec` to seconds `tv_sec` without scaling either one to the other. Your adding a (likely) small number (the seconds) to a likely big number (the micro seconds), so you get a big number as a result. For a specific example on how to substract to timevals, see [this](http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html). – Christian.K Jun 14 '16 at 09:54
  • thank you, helped a lot. – wackyburkay Jun 14 '16 at 11:40

0 Answers0