Timer to find elapsed time in a function call in C

后端 未结 11 1652
故里飘歌
故里飘歌 2020-12-09 14:20

I want to calculate time elapsed during a function call in C, to the precision of 1 nanosecond.

Is there a timer function available in C to do it?

If yes ple

11条回答
  •  余生分开走
    2020-12-09 14:51

    Any timer functionality is going to have to be platform-specific, especially with that precision requirement.

    The standard solution in POSIX systems is gettimeofday(), but it has only microsecond precision.

    If this is for performance benchmarking, the standard way is to make the code under test take enough time to make the precision requirement less severe. In other words, run your test code for a whole second (or more).

提交回复
热议问题