Improve code running efficiency (1)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

In the following blog, I will explain some ways to improve the efficiency of personal code. These methods are proven to be practical. However, different processors and processing platforms may differ, but they are essentially the same.

 

(1) Use for (; instead of while (1)

(2) The internal data is first recycled during the cycle, and then the external data is recycled.

(3) only related operations of the same data should be arranged within the cycle of the same layer.

(4) do not include the header files during compilation. It should be as simple as possible.

(5) Try not to use multiplication and division, and use addition, subtraction, and shift operations.

(6) Some replication and computing operations can be replaced by SIMD commands, such as SSE commands.

(7) If it is server software or game client software, please use more queries and less computing

(8) When if ()... else () is used, put the code that appears the longest before, and the result that does not appear frequently at the end.

(9) When arrays are used, int * P = & value [0]; P ++ is used for iteration, which can reduce the calculation of data.

(10) optimization algorithms to leverage the advantages of multiple CPU cores and maximize the speed limit of CPU features

 

 

Note:

All the following code is completed by vc6.0.

 

Details:

(1) Why do we need to use for (;) instead of while (1 )?

 

5: int m = 0; <br/> 00401038 mov dword ptr [ebp-4], 0 <br/> 6: <br/> 7: While (1) <br/> 0040103f mov eax, 1 <br/> 00401044 test eax, eax <br/> 00401046 je test + 32 h (00401052) <br/> 8: {<br/> 9: If (M = 0) <br/> 00401048 cmp dword ptr [ebp-4], 0 <br/> 0040104c JNE test + 30 h (00401050) <br/> 10: break; <br/> 0040104e JMP test + 32 h (00401052) <br/> 11 :}< br/> 00401050 JMP test + 1fh (0040103f) <br/> 12: <br/> 13: For (;) <br/> 14: {<br/> 15: If (M = 0) <br/> 00401052 cmp dword ptr [ebp-4], 0 <br/> 00401056 JNE test + 3ah (0040105a) <br/> 16: break; <br/> 00401058 JMP test + 3ch (0040105c) <br/> 17 :}< br/> 0040105a JMP test + 32 h (00401052) <br/>

 

It can be clearly seen that while (1) is translated into three commands, but for (;) is not. Many may think that this is only three instructions, and there is no big fuss,

However, we need to know that many cycles are carried out over times. If the average function pays attention to this problem, the CPU time saved in one day is considerable.

 

 

(2) to be continued

 

 

 

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.