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, limited, numlow; // Get the value of the CPU internal counter at the start of the code __ASM {RDTSC MOV HighStart, Edx Mov LowStart, Eax} for (INT i = 0; I <100000; I ) {for (INT i = 0; I <100000; i ) {}} // CPU inside the CPU The value of the counter, and subtracts the initial value __asm {// rdtsc // Mov highend, EDX // MOV LOWEND, EAX / / (get two counters worth paying // ----------- ------------------------------------- // Sub eax, lowstart // cmp EAX, 0; If the difference between 32 is negative, it is negative, because the second time is always longer than the first large // jmp L2 // L1: Mov Numlow, Eax // L2: SBB EDX, HIGHSTART / / MOV NUMHIGH, EDX / / ----------------------------------------- --------------- // The previous code has a logical error. In fact, it can be done directly with SBB, but don't have to consider whether low 32 bits above high 32 RDTSC MOV HIGHEND, EDX MOV LOWEND EAX SUB EAX, LOWSTART MOV NUMLOW, EAX MOV EAX, EDX SBB EAX, HIGHSTART MOV NUMHIGH, EAX} // Put the difference between the two counter values in a 64-bit plastic variable // Put the 32-bit left shift 32 bits in the shaped variable of 64, then add low 32-bit __int64 time = 0; time | = NumHigh; Timer = Timer << 32; time | = numlow; // output code segment Running clock cycle // Take the frequency 1.1GCPU, if you change the computer to change the 1.1 multiply, because everyone's CPU should be 1G or more ^ _ ^ cout << (Double) (Timer / 1.1 / 1000000000) << Endl; 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. ^ _ ^ --------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------ Discovery procedure There is an error: When the value of the first acquisition is 0xffffff, the value of the last output of 0x00000000 in the second time is definitely negative. For a CPU, the maximum time of time is 0xFffffFFFF / CPU frequency (in G) / 1000000000s. So can only be used to calculate the code that the run time is less than the maximum time may be correct (it is also possible). Also, if it is a double CPU, what is the problem? ? ? How can I solve the problem mentioned above? ? Thinking ... I would like to thank IPLinger to make a procedure in the process, and put forward my own ideas, I have made me discovered the mistake, thank you here! November 10, 2004