How to calculate the execution time in C?

后端 未结 4 1312
自闭症患者
自闭症患者 2021-01-27 09:38

How can I calculate the execution time in the following code:

#include   /* Core input/output operations                         */
#include 

        
4条回答
  •  萌比男神i
    2021-01-27 10:29

    n_procs is never initialized, the 16372-value that gets printed just happens to be what was previously on the stack.

    The C standard library doesn't provide functionality to query processor count or high-performance timers, so you will have to look at other means of querying this. For instance, both POSIX and Windows API provides functionality like this.

    edit: See Programmatically find the number of cores on a machine for how to initialize n_procs. Seeing how you use gettimeofday, you're probably on some unix-variant; "n_procs = sysconf(_SC_NPROCESSORS_ONLN);" is probably what you want.

提交回复
热议问题