Calculating Function time in nanoseconds in C code

前端 未结 4 1208
栀梦
栀梦 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 07:59

    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.

提交回复
热议问题