C ++ calculates the running time of a program, accurate to milliseconds _ Baidu Knows
C ++ calculatesProgramRun time, accurate to milliseconds
5201314 | category: C/C ++ | 2107 views
Use clock_t to declare two variables start and finish;
Start = clock ()
// Run a function
Finish = clock ()
The resulting finish-start is the time used to execute this function. What is the unit? What about seconds? What about milliseconds? Or microseconds? In some cases, what does clk_tck or clocks_per_sec mean? How can we get millisecond or subtle precision?
Let me help.
Submitted
Clock () returns the CPU time unit, while clocks_per_sec indicates the number of time units in a second. Therefore, the correct running time is (finish-Start)/clocks_per_sec, in this way, we can get the number of seconds of execution, multiply the number of milliseconds by 1000.0, and multiply the number of milliseconds by 1000.0.
Question
What if you want to get the result in milliseconds: Finish-start or (finish-Start)/clocks_per_sec * 1000? Why is it a little different from downstairs?
Answer
Here we can see the value of clocks_per_sec. The value of clocks_per_sec defined in VC is 1000, so
Either finish-start or (finish-Start)/clocks_per_sec * 1000.0 can get milliseconds