C ++ calculates the running time of a program, accurate to milliseconds

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.