High-performance coding rules

Source: Internet
Author: User

1. byte boundary alignment improves read/write speed

2. the constructor tries its best to use the initialization list (otherwise, the object type will have an initial value constructed by default)

3. Try to use the front ++, because the back ++ will generate a temporary object.

4. Do not use struct function return values

Struct;

A = F (1 );

A temporary object is generated.

Struct a = F (1); this will not generate a temporary object

5. Avoid constructing with a value (you can add explicit it before the constructor to avoid-implicit conversion is not allowed)

STD: String S = "hello"; the actual operation of this sentence is S = STD: String temp ("hello ");

A temporary object is generated. The correct method is STD: String S ("hello"). You can call the constructor directly.

6. Simplified processing in the loop (1) repeated operations must be put out of the loop (2) the busiest loop should be put in it)

7. Avoid recursion as much as possible. It is best to convert it into a loop (must be used, and a definite exit condition is required)

 

8. Use less floating-point numbers (convert * 10, * 100 to an integer)

9. Pass pointer or reference using function parameters (complex structure) ---- avoid copying Structures

10. Do not format I/O using strings in the Key Path (string formatting is slow)

11. do not apply for memory at Will (if new is available, do not use new). Use the stack memory.

The string value is a dynamic size. When the space is insufficient, the new memory is added. The C language string is much faster than the C ++ string value.

 

12. Use sizeof (computed during compilation), instead of strlen (computed during runtime)

Char A [10] = "hello ";

Sizeof ();

Strlen ();

Result: 10 5

 

13. frequent application of heap space smaller than kb will lead to memory expansion

Because small memory blocks are directly implemented through the low pointer of the Offset heap, only the latest memory is released will the previous memory be released. Continuous application will lead to expansion.

14. Use STL containers with caution-Misuse of Small memory blocks in the heap Area

15. Select the appropriate container for the vector deque list

STD: vector is a continuous space. The memory block needs to be moved for addition and deletion, and the memory needs to be re-allocated.

Deque concatenates multiple fixed-length heap arrays.

STD: List Bidirectional Ring linked list

Map

Hash table

  

16. Multithreading

Mutex lock

Read/write lock

Conditional lock

Spin lock

Barrier lock

17. Efficient I/O kqueue epoll

 

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.