Timer to find elapsed time in a function call in C

后端 未结 11 1647
故里飘歌
故里飘歌 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

    On Intel and compatible processors you can use rdtsc instruction which can be wrapped into an asm() block of C code easily. It returns the value of a built-in processor cycle counter that increments on each cycle. You gain high resolution and such timing is extremely fast.

    To find how fast this increments you'll need to calibrate - call this instruction twice over a fixed time period like five seconds. If you do this on a processor that shifts frequency to lower power consumption you may have problems calibrating.

提交回复
热议问题