11

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 functions in time.h that can be used to calculate the time in nanoseconds?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Mousa Farajallah
  • 167
  • 1
  • 1
  • 11
  • Accuracy to the nanosecond is at best highly platform dependent. What platform - Windows/Linux/something embedded? – Steve Fallows Nov 28 '12 at 17:06
  • No, and measuring a single function call isn't very useful anyways. You could use clock() though. – Cubic Nov 28 '12 at 17:07
  • 2
    If you are using linux, Linux is not real time platform. and its scheduler scale is in milliseconds. – MOHAMED Nov 28 '12 at 17:10
  • 1
    Try do a profiling on the program to get a relative time of each function. – 1-----1 Nov 28 '12 at 17:17
  • 1
    C11 becomes available `timespec_get` will do it: http://stackoverflow.com/a/36095407/895245 linux version of this question: http://stackoverflow.com/questions/16275444/how-to-print-time-difference-in-accuracy-of-milliseconds-and-nanoseconds – Ciro Santilli OurBigBook.com Mar 18 '16 at 23:15
  • Does this answer your question? [How can I get the Windows system time with millisecond resolution?](https://stackoverflow.com/questions/3729169/how-can-i-get-the-windows-system-time-with-millisecond-resolution) – phuclv Apr 30 '22 at 15:18

4 Answers4

12

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.

Community
  • 1
  • 1
chacham15
  • 13,719
  • 26
  • 104
  • 207
  • timespec_get from C11 returns up to nanoseconds, rounded to the resolution of the implementation. check out http://stackoverflow.com/a/36095783/2680660 – Efreeto May 11 '16 at 17:48
  • @efreet I didnt say that you couldnt get a nanosecond return value, I said that it wouldnt be accurate and therefore no more useful than a millisecond return value. – chacham15 May 13 '16 at 18:23
3

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;
  }
Cloud
  • 18,753
  • 15
  • 79
  • 153
1

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • note: there is a difference between "nanosecond resolution" and "nanosecond accuracy". furthermore, he wants a windows solution, which this is not (sorry, he mentioned it in a comment, I just added the tag). – chacham15 Nov 28 '12 at 18:32
0

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

Community
  • 1
  • 1
Alex I
  • 19,689
  • 9
  • 86
  • 158