Using clock() to measure execution time

五迷三道 提交于 2019-11-29 11:04:49

For durations like two hours, I wouldn't be too concerned about clock(), it's far more useful for measuring sub-second durations.

Just use time() if you want the actual elapsed time, something like (dummy stuff supplied for what was missing):

#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

clock measures the cpu time and not the wallclock time. Since you are not running the majority of your code on the cpu, this is not the right tool.

Jeyaram

gettimeofday() function also can be considered.


The gettimeofday() function shall obtain the current time, expressed as seconds and microseconds since the Epoch, and store it in the timeval structure pointed to by tp. The resolution of the system clock is unspecified.


Calculating elapsed time in a C program in milliseconds

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!