Acquire code running time in VC ++ through compilation

xiaoxiao2021-03-06  48

Acquire code running time in VC through compilation

How to get the program or a piece of code running? You may say that there is a special program test tool, but you can also embed the assembly code in the program. In the Pentium's instruction system, there is an instruction to get the value of the 64-bit counter inside the CPU. We can get the number of clock cycles of the program or code run through the code twice to obtain the value of the counter.

The frequency of your CPU calculates a clock cycle, thus calculating the exact time of the program operation. We get the value of the CPU internal counter by instruction TDSIC, and the command TDSIC returns in EDX: EAX, where the 64-bit register is stored in the 64-bit register, and Eax stores the value of the 32nd bit.

Let's take a look at the code: =========================================== ============================================================================================================================================================================================================= // Realize the time to obtain a piece of code

#include

Using namespace std;

Void getClockNumber (long high, long low); void getRuntime (); int main () {long highstart, lowstart, highend, lo down; long number, numlow; // Get code run start CPU internal counter value __asm ​​{RDTSC MOV HighStart, EDX MOV LOWSTART, EAX} for (INT I = 0; I <100000; I ) {for (INT i = 0; I <100000; i ) {}} // Get the value of the CPU internal counter at the end of the code, And subtract the initial value __asm ​​{RDTSC MOV HIGHEND, EDX MOV LOWEND, EAX; get two counters worth poor SUB EAX, LOWSTART CMP EAX, 0; if the difference in 32 is negligible, because the second time is always longer than The first big JG L1 NEG EAX JMP L2 L1: MOV NUMLOW, EAX L2: SBB EDX, HIGHSTART MOV NUMHIGH, EDX} // Put the difference between the two counter values ​​in a 64-bit plastic variable // High 32-bit left shift 32 bits are placed in a shaped variable of 64, then add a low 32-bit __int64 time = (NumHigh << 32) Numlow; // Output code segment running clock cycle number // Taking frequency 1.1GCPU Take an example, if the computer is changed to change the 1.1 to other other, because it is believed that everyone's CPU should be 1G or more ^ _ ^ cout << (double) (Timer / 1.1/10000000) << end1; return 0;} This allows for a probably time of program or a code, but does not get the exact time of operation, because even if the middle cycle is removed, the program will have a run time.

Because after the first acquisition of the value of the counter, two assembly instructions MOV HighStart, EDX MOV LOWSTART, EAX These two instructions are of course running time, of course, you can lose these two instructions.

Time (on 1.1G machine is 3e-8s), this will be more accurate. ^ _ ^ If you want to know the runtime of the program, the professional test software will definitely be better, but it seems that there is no need to get the program unless specialized. However, it is also good to DIY, no matter whether there is

No, at least you can learn how to embed the assembly code in VC , how to use 32-bit registers, actually used in the 16-bit register, will be the same in the future, but the number of bits is different.

转载请注明原文地址:https://www.9cbs.com/read-117691.html

New Post(0)