C ++ Embedded Assembler Program improves computing efficiency

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.