How to calculate the execution time in C?

落花浮王杯 提交于 2019-12-02 07:39:11

Because you don't have adequate format strings for them, you need something starting with a '%', like:

printf("    Time   :%d \n", clock() );
kusma

n_procs is never initialized, the 16372-value that gets printed just happens to be what was previously on the stack.

The C standard library doesn't provide functionality to query processor count or high-performance timers, so you will have to look at other means of querying this. For instance, both POSIX and Windows API provides functionality like this.

edit: See Programmatically find the number of cores on a machine for how to initialize n_procs. Seeing how you use gettimeofday, you're probably on some unix-variant; "n_procs = sysconf(_SC_NPROCESSORS_ONLN);" is probably what you want.

Try this:

printf("    Time   : %lu\n", clock() );
printf("    Start Time   : %lds %ldus\n", tv1.tv_sec, tv1.tv_usec);
printf("    End Time   : %lds %ldus\n", tv2.tv_sec, tv2.tv_usec);

And for:

double timeval_subtract (result, x, y)

use the following to return the time difference in micro seconds:

long timeval_subtract (struct timeval * result, struct timeval * x, struct timeval * y)
{
   long usec = x->tv_sec * 1000000L + x->tv_usec;
   usec -= (y->tv_sec * 1000000L + y->tv_usec);

   result->tv_sec = usec / 1000000L;
   result->tv_usec = usec % 1000000L;

   return usec;
}

Depending on the difference of the two dates x and y the return value of the function timeval_subtract (not the value represented by result!) might be wrong, due to an overflow.

Assuming a long is 32bit wide this overflow will occur with differences larger than 4294s, for a long having 64bit (which should be the case an 64bit machines) the overflow whould occur after much later ... ;-)

I'd try the following :

int     timeval_subtract ( struct timeval *result, struct timeval *x, struct timeval *y ) {

    if ( x->tv_usec < y->tv_usec ) {
            int nsec = ( y->tv_usec - x->tv_usec ) / 1000000 + 1;
            y->tv_usec -= 1000000 * nsec;
            y->tv_sec += nsec;
    }
    if (x->tv_usec - y->tv_usec > 1000000) {
            int nsec = ( x->tv_usec - y->tv_usec ) / 1000000;
            y->tv_usec += 1000000 * nsec;
            y->tv_sec -= nsec;
    }

    result->tv_sec = x->tv_sec - y->tv_sec;
    result->tv_usec = x->tv_usec - y->tv_usec;

    return x->tv_sec < y->tv_sec;
}

void Start ( struct timeval *timer_profiling ) {
        if ( timer_profiling == NULL )   return;
        gettimeofday ( timer_profiling , NULL );
        return;
}

void End ( struct timeval *timer_profiling , char *msg ) {
        struct timeval res;
        struct timeval now;
        gettimeofday ( &now , NULL );

        if ( msg == NULL )      return;

        timeval_subtract ( &res , &now , timer_profiling );
        sprintf ( msg , "[ %ld,%.3ld ms]" , res.tv_sec*1000 + (long)round(res.tv_usec/1000) , res.tv_usec - (long)round(res.tv_usec/1000)*1000);

        return;
}

Start(&s) one with an allocated timer_profiling , then retrieve the result in a string by calling End(&s,buff);

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