As assembly language is closer to the hardware level than C + +, it is often possible to take the form of embedded assembly in C + + code to speed up the program in high performance requirements.
In the VC can be simple through
__asm{//Add assembly code here}
to achieve. The following is a very simple example of the performance gap between Assembly cross-compilation and direct C + + code, with the following code and running results:
#include <stdio.h> #include "time.h" #define Numberofcalculation 10000000void Main () {Long gettime = clock (); unsigned int i = numberofcalculation;unsigned int a1 = 1, B1 = 2;unsigned int a2 = 1, b2 = 2;while (i--) {//CA1 = a1 + b1;b 1 = a1 + B1;} printf ("a:%d b:%d\n", A1, B1);p rintf ("Ordinary C + + program spends time%dms\n", clock ()-gettime); i = Numberofcalculation;gettime = Clock (); while (i--) {//Assembler __asm{mov eax, A2;mov ebx, B2;add eax, Ebxmov A2, Eax;add ebx, Eaxmov B2, ebx;}} printf ("a:%d b:%d\n", A2, B2);p rintf ("Assembly cross-compiler spends time%dms\n", clock ()-gettime); GetChar ();}
If you are in a complex program, the difference in performance will be more visible.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + embedded Assembler improves computational efficiency