How can I accurately calculate the CPU running time of an algorithm?

Source: Internet
Author: User

Http://community.csdn.net/Expert/FAQ/FAQ_Index.asp? Id = 211457

 
How accurate do you need?
Gettickcount can be 18-20 ms
Timegettime can reach 1 ms precision
......
 
Of course, these are not supported by the C or C ++ standard.
 
Then we will sacrifice the best way,
Reads the number of machine cycles executed since the CPU is started,
An assembly command: rdtsc (read timestamp count)
The precision can reach the NS level. (Accurately speaking, the precision is 1/the clock frequency of your CPU, which is also the limit)
 
 
 

Long highstart, lowstart, highend, lowend;
Long numhigh, numlow;

_ ASM
{
Rdtsc
MoV highstart, EDX
MoV lowstart, eax

// Put your time-consuming code here ......
Rdtsc
MoV highend, EDX
MoV lowend, eax
// Obtain the counter value difference twice
Sub eax, lowstart
CMP eax, 0
JG L1
Neg eax
JMP L2

L1: mov numlow, eax
L2: SBB edX, highstart
MoV numhigh, EDX

}
_ Int64 timer = (numhigh <32) + numlow; // get the final result

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.