From time to time, I will summarize what simple syntaxes I think can speed up the program.

Source: Internet
Author: User

The summary in this article is my personal opinion and remains to be discussed. I hope you will discuss it more.

 

1. A ++ and ++

Use ++ a whenever possible, because in general, ++ A is faster than a ++.

Why? Because the implementation of ++ A is to first add the aplus 1 (INC command), and then transfer the address of a; and the implementation of a ++, also, copy a to memory unit B first, then a + 1 (INC command), and then transfer the address of B. Therefore, if a ++ is compared with ++ a, a duplicate copy will reduce the speed.

 

2. Const modification is faster than non-const modification.

Obviously, when the compiler is optimizing, the const object is not modified, but can be put forward and reused. Therefore, the optimization rate is obviously higher than that of non-const, and the speed will be correspondingly improved.

 

3. Try to use the switch as much as possible, because the switch is not a simple if-Else, and the switch speed is close to goto!

Because the switch is to generate all the code in the same region, and then hash the code to a running position based on different cases. Therefore, it is constantly compared with if-Else, the speed is obviously greatly improved. This also explains why the switch can only use the int type, because the hash of other types is not obvious, which is also the reason for break, because there is no break, the code will be continuously executed in the program segment produced by the switch, and will jump out only when break is encountered.

 

All of the above are discovered by myself implementing the compiler or reading some compilation books, which need to be discussed, but basically I have done experiments and should be consistent

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.