Using clock () to measure runtime

I am running a C program using GCC and the simplest cross-compiler DSP to simulate some functionality. I use the following code to measure the execution time of a specific part of my program:

clock_t start,end;
printf("DECODING DATA:\n");
start=clock();
conv3_dec(encoded, decoded,3*length,0);
end=clock();
duration = (double)(end - start) / CLOCKS_PER_SEC;
printf("DECODING TIME = %f\n",duration);

where conv3_dec()is the function defined in my program and I want to find the execution time of this function.

Now, when my program starts, the functions conv3_dec()run for almost 2 hours, but the conclusion from it printf("DECODING TIME = %f\n",duration)says that the function was completed in just half a second ( DECODING TIME = 0.455443). This is very confusing for me.

I used the method clock_tto measure the execution time of programs before, but the difference has never been so huge. This is due to the cross compiler. Just like a note, the simulator simulates a DSP processor running at just 500 MHz, just like the difference in clock speed of the DSP processor, and my processor causing the error, measures CLOCKS_PER_SEC.

+3
source share
3 answers

For a duration of, for example, two hours, I would not worry too much about it clock(), it is much more useful for measuring the duration of the second second.

Just use time(), if you want the actual past tense, something like (dummy material provided for absent):

#include <stdio.h>
#include <time.h>

// Dummy stuff starts here
#include <unistd.h>
#define encoded 0
#define decoded 0
#define length 0
static void conv3_dec (int a, int b, int c, int d) {
    sleep (20);
}
// Dummy stuff ends here

int main (void) {
    time_t start, end, duration;
    puts ("DECODING DATA:");
    start = time (0);
    conv3_dec (encoded, decoded, 3 * length, 0);
    end = time (0);
    duration = end - start;
    printf ("DECODING TIME = %d\n", duration);
    return 0;
}

which generates:

DECODING DATA:
DECODING TIME = 20
+4

clock , . , .

+4

gettimeofday() can also be considered.


The gettimeofday () function should get the current time, expressed in seconds and microseconds since the Age, and store it in the timeval structure pointed to by tp. The resolution of the system clock is not defined.


Calculating elapsed time in program C in milliseconds

http://www.ccplusplus.com/2011/11/gettimeofday-example.html

+2
source

Source: https://habr.com/ru/post/1691354/


All Articles