Calculating Function time in nanoseconds in C code

前端 未结 4 1104
栀梦
栀梦 2020-12-19 07:29

I need to know how can I calculate the time of a function in C code in nanoseconds. I tried to repeat the function until consume some microseconds. Are there any other funct

相关标签:
4条回答
  • 2020-12-19 07:48

    Regardless of how you approach this or what type of system/OS you are using, you are getting an approximate answer at best, with considerable variance due to the nature of the problem.

    Second, you need a system that supports this kind of call. It's pretty easy if you're using QNX Neutrino:

    http://www.qnx.com/developers/docs/6.3.0SP3/neutrino/lib_ref/c/clock_gettime.html

    /*
     * This program calculates the time required to
     * execute the program specified as its first argument.
     * The time is printed in seconds, on standard out.
     */
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <time.h>
    
    #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" );
          return EXIT_FAILURE;
        }
    
        system( argv[1] );
    
        if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
          perror( "clock gettime" );
          return EXIT_FAILURE;
        }
    
        accum = ( stop.tv_sec - start.tv_sec )
                 + (double)( stop.tv_nsec - start.tv_nsec )
                   / (double)BILLION;
        printf( "%lf\n", accum );
        return EXIT_SUCCESS;
      }
    
    0 讨论(0)
  • 2020-12-19 07:56

    Call the function enough times that you get total time in the seconds, and use any method to measure (even plain C clock()). Measuring in micro/nanoseconds is inherently too imprecise.

    For benchmarking, you want QueryPerformanceCounter. It is the best stopwatch available on Windows. See: How to use QueryPerformanceCounter? However using this to measure (for example) a single function call will still be inherently very imprecise; I think you need to time on the order of a second total to get good signal-to-noise ratio for your measurements. Take multiple measurements of whatever duration you want and compare their value to their standard deviation to make sure you are measuring sufficiently accurately.

    Multimedia Timers are probably the best clock (note the difference between clock and stopwatch: one measures absolute time, the other time intervals only) available on Windows. You should be able to get near-millisecond resolution and precision with timeGetTime().

    0 讨论(0)
  • 2020-12-19 07:59

    You are never going to get nanosecond accuracy. Think about what you are asking: on a 1 GHz CPU 1 nanosecond is a clock cycle. No matter what you attempt to call, you will never get that kind of accuracy, you are better off sticking to microseconds. A similar question with many examples is here: C++ Cross-Platform High-Resolution Timer.

    For c only: on windows you want to use the QueryPerformanceCounter. And here is more on QPC. Here is a related question on how to use QueryPerformanceCounter.

    0 讨论(0)
  • 2020-12-19 08:01

    The clock function in standard C is not useful for this. It usually has horrible resolution and it's inconsistent (between platforms) whether it measures elapsed wall time or cpu time consumed. You should use the POSIX-standard clock_gettime function (which has nanosecond resolution and lets you specify which clock you want to measure against) and emulate it with whatever system-specific clock operations are available on platforms that lack the POSIX function.

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