How can I find the execution time of a section of my program in C?

后端 未结 11 1944
花落未央
花落未央 2020-12-31 05:23

I\'m trying to find a way to get the execution time of a section of code in C. I\'ve already tried both time() and clock() from time.h, but it seems that time() returns seco

11条回答
  •  既然无缘
    2020-12-31 05:40

    It depends on the conditions.. Profilers are nice for general global views however if you really need an accurate view my recommendation is KISS. Simply run the code in a loop such that it takes a minute or so to complete. Then compute a simple average based on the total run time and iterations executed.

    This approach allows you to:

    1. Obtain accurate results with low resolution timers.

    2. Not run into issues where instrumentation interferes with high speed caches (l2,l1,branch..etc) close to the processor. However running the same code in a tight loop can also provide optimistic results that may not reflect real world conditions.

提交回复
热议问题