Calculating Function time in nanoseconds in C code

前端 未结 4 1103
栀梦
栀梦 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 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.

提交回复
热议问题