Easily measure elapsed time

前端 未结 26 2832
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

26条回答
  •  情书的邮戳
    2020-11-22 04:47

    On linux, clock_gettime() is one of the good choices. You must link real time library(-lrt).

    #include 
    #include 
    #include 
    #include 
    
    #define BILLION  1000000000L;
    
    int main( int argc, char **argv )
      {
        struct timespec start, stop;
        double accum;
    
        if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
          perror( "clock gettime" );
          exit( EXIT_FAILURE );
        }
    
        system( argv[1] );
    
        if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
          perror( "clock gettime" );
          exit( EXIT_FAILURE );
        }
    
        accum = ( stop.tv_sec - start.tv_sec )
              + ( stop.tv_nsec - start.tv_nsec )
                / BILLION;
        printf( "%lf\n", accum );
        return( EXIT_SUCCESS );
      }
    

提交回复
热议问题