High-Precision CPU timestamp in C ++ (in nanoseconds)

Source: Internet
Author: User

For performance-focused program developers, a good timing component is both a mentor and a mentor. Timers can be used as program components to help programmers precisely control program processes, and are also a powerful debugging weapon. experienced programmers can determine program performance bottlenecks as soon as possible, or make a convincing performance comparison for different algorithms.

On Windows, there are two commonly used Timers: timegettime multimedia timer, which provides millisecond-level timer. However, this accuracy is still too rough for many applications. The other is the queryperformancecount counter, which provides a microsecond-level count as the system differs. For real-time graphics processing, multimedia data stream processing, or real-time system construction programmers, using queryperformancecount/queryperformancefrequency is a basic skill.

(Reference )--

 

We can write in C ++ as follows:

 

 

_ Declspec (naked) unsigned _ int64 getcpucycle (void)
{
_ ASM
{
Rdtsc
RET
}
}

 

The returned values of rdtsc are stored in edX eax. edX is high 32-bit, and eax is low 32-bit...

 

 

Here _ declspec (naked) is not required .. As mentioned above. The key is the rdtsc command (read time stamp counter) to obtain the high-precision timestamp of the CPU. You can also write as follows:

_ Declspec (naked) unsigned _ int64 getcpucycle (void)
{
_ ASM
{
_ ASM _ emit 0x0f
_ ASM _ emit 0x31
RET
}
}

 

This is equivalent to the machine code that is directly pushed into the command. The original post said that rdtsc cannot be recognized in the C ++ inline assembly compiler. I can do it anyway. I don't know if the compiler can identify the cause. I will not discuss this here --

 

In this way, we can obtain the current number of CPU cycles from power-on everywhere:

Unsigned _ int64 icpucycle = getcpucycle ();

 

Based on this number, we can calculate the time (s) that has elapsed since power-on ):

Second = icpucycle/CPU clock speed (HZ );

1 GHZ = 1,000 MHZ = 1,000,000 KHZ = 1,000,000,000 HZ;

 

You can rest assured that an unsigned _ int64 will not overflow-you can calculate how many years your CPU can be stored ..

 

There are several advantages based on this method: First, high precision, second, minimum function call overhead, third, low platform limitations, and fourth, there is a direct relationship with CPU clock speed... However, due to the high precision, the numbers are relatively large ..

 

With such a timer, some real-time processing is more accurate .... --

 

 

Related Article

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.