C + + Get CPU frequency (RPM)

Source: Internet
Author: User
Tags sleep function

Longlong getfrequency (DWORD sleeptime)//Get CPU Frequency{DWORD Low1=0, HIGH1 =0, Low2 =0, HIGH2 =0;    Large_integer Fq, St, Ed; /*the QueryPerformanceFrequency () function should be called before timing to obtain the clock frequency of the internal timer of the machine. Then QueryPerformanceCounter () is called before and after the event that requires strict timing, and the exact time elapsed can be calculated using the difference in technology and the frequency of the clock two times. */:: QueryPerformanceFrequency (&AMP;FQ);//Precise timing (returns the frequency of hardware-supported high-precision counters):: QueryPerformanceCounter (&AMP;ST);//Get start time__asm {//the number of times to obtain the current CPURDTSC mov low1, eax mov high1, edx}:: Sleep (Sleeptime); //suspend a thread for a moment:: QueryPerformanceCounter (&ed);//Get end Time__asm {RDTSC//Read the CPU timestamp countermov low2, eax mov high2, edx}//Convert CPU time period to 64-bit integerLonglong begin= (Longlong) high1 << +|Low1; Longlong End= (Longlong) high2 << +|Low2; //divide the number of CPU cycles obtained two times by the interval time, that is, the frequency of the CPU//because the sleep function of Windows has an error of approximately 15 milliseconds, the precise timing of windows will prevail    return(End-begin) *fq. QuadPart/(ed. QuadPart-St. QuadPart);}

Transferred from: http://blog.csdn.net/kofandlizi/article/details/6253801

C + + Get CPU frequency (RPM)

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.