C ++ Embedded Assembler Program improves computing efficiency
Since the Assembly Language is closer to the underlying hardware than the C ++ language, in programs with high performance requirements, the compilation method can be embedded in the C ++ code to speed up the program.
In VC, you can simply use
_ Asm {// Add assembly code here}. The following is a very simple example to illustrate the performance gap between Assembly Cross-compilation and direct C ++ code. The code and running results are as follows:
# Include
# Include time. h # define NumberOfCalculation 10000000 void main () {long gettime = clock (); unsigned int I = NumberOfCalculation; unsigned int a1 = 1, b1 = 2; unsigned int a2 = 1, b2 = 2; while (I --) {// a1 = a1 + b1; b1 = a1 + b1;} printf (a: % d B: % d, a1, b1); printf (common c ++ program spends time % dms, clock ()-gettime); I = NumberOfCalculation; gettime = clock (); while (I --) {// assemble _ asm {mov eax, a2; mov ebx, b2; add eax, ebxmov a2, eax; add ebx, eaxmov b2, ebx ;}} printf (: % d B: % d, a2, b2); printf (Assembly Cross-Compilation Program time % dms, clock ()-gettime); getchar ();}
In a complex program, the performance difference is more obvious.