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 .... --