According to MSDN information http://support.microsoft.com/default.aspx?scid=kb;n-us; 172338, the most accurate timer provided by theAPI is to use QueryPerformanceCounter, on the machine of our company Celeron2.4g The timing unit by QueryperFormanceFrequency is approximately 2.79E-7. If you need to quite accurate, you need to use the way to get the CPU clock cycle, 2.4G, the clock cycle is about 4.16e-10, which can be met High requirements.
The article found online is VC version, and now it will change it to the GCC version.
GCC embedded ASM can be used in the following format:
__ASM__ (Instruction
: OUTPUT
: INPUT
: Clobbed)
The code for obtaining the TIME-Stamp Counter count value using RDTSC is as follows:
Unsigned long high32, low32
__ASM__ ("RDTSC"
: "= A" (low32), "= D" (high32)
)
Unsigned long long counter = high32;
Counter = (Counter << 32) low32
Counter is the current Time-Stamp Counter count value, the timing unit is the CPU clock cycle.
The way to get the CPU clock cycle is that the CPU frequency can be calculated, the 2.4G CPU is 1 / 2.4e 9 seconds. The problem is to get the frequency of the current CPU. According to I know, the frequency of the P4 series CPU can obtain the processor brand string through the CPUID instruction, and the processor brand string contains the frequency information of the CPU, the code is as follows:
// 0x80000002, 0x80000003, 0x80000004 You can get the full processor brand string and the frequency of the Processor Brand String obtained by 0x80000004 is frequency
Long op = 0x80000004;
Long Eax, EBX, ECX, EDX;
__ASM __ ("MOV %% EAX,% 0 / N / T"
"Cpuid"
: "= A" (EAX), "= B" (EBX), "= C" (ECX), "= D" (EDX)
: "R" (OP)
)
In order to read the EAX, EBX, ECX, and EDX, follow the ASCII code, is the information of the CPU
E.g
CHAR CH;
For (int i = 0; i <4; i ) {
CH = EAX >> (i * 8);
Cout << CH;
}
In the EDX in my machine, ECX is 2.40, which is 2.40, this is taken to the frequency of the CPU.
But this method is not universal, not all CPUs can take the frequency. There is also the current Pentium M changes in the current, and the specific situation is to be tested.
There is another way to calculate the clock cycle in the program, and then calculate the clock cycle according to the count value before and after, but this method is not accurate because the number of clock cycles used by the call and the SLEEP itself are very It's hard to calculate, if you don't count, the error is great.
BTW: The IDE used is DEV C , as a free IDE, it should be good, but the more it is more uncomfortable, and finally, it will be used to calculate> o <, compared to the Eclipse development Java is really awesome. However, this also has the reason for the language itself and GCC.
About the ASM instructions can check Intel's information http://www.intel.com/design/pentium4/Manuals/index_new.htm
Author Blog:
http://blog.9cbs.net/alexlex/