How to calculate the execution time in C?

后端 未结 4 1263
自闭症患者
自闭症患者 2021-01-27 09:38

How can I calculate the execution time in the following code:

#include   /* Core input/output operations                         */
#include 

        
相关标签:
4条回答
  • 2021-01-27 10:16

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

    0 讨论(0)
  • 2021-01-27 10:24

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

    printf("    Time   :%d \n", clock() );
    
    0 讨论(0)
  • 2021-01-27 10:28

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

    0 讨论(0)
  • 2021-01-27 10:29

    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.

    0 讨论(0)
提交回复
热议问题