Calculating function time in nanoseconds
Asked Answered
L

4

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?

Loblolly answered 28/11, 2012 at 17:1 Comment(6)
Accuracy to the nanosecond is at best highly platform dependent. What platform - Windows/Linux/something embedded?Heilman
No, and measuring a single function call isn't very useful anyways. You could use clock() though.Onstage
If you are using linux, Linux is not real time platform. and its scheduler scale is in milliseconds.Maestoso
Try do a profiling on the program to get a relative time of each function.Dispose
C11 becomes available timespec_get will do it: https://mcmap.net/q/20896/-how-to-measure-time-in-milliseconds-using-ansi-c linux version of this question: stackoverflow.com/questions/16275444/…Copious
Does this answer your question? How can I get the Windows system time with millisecond resolution?Thuggee
C
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.

Capsulate answered 28/11, 2012 at 17:30 Comment(2)
timespec_get from C11 returns up to nanoseconds, rounded to the resolution of the implementation. check out https://mcmap.net/q/21116/-how-to-print-time-difference-in-accuracy-of-milliseconds-and-nanoseconds-from-c-in-linuxSkimmia
@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.Capsulate
S
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;
  }
Synchronous answered 28/11, 2012 at 17:37 Comment(0)
W
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.

Woollen answered 28/11, 2012 at 18:19 Comment(1)
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).Capsulate
S
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().

Sherborne answered 28/11, 2012 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.