Efficiency issues in C ++ Programming

Source: Internet
Author: User

1. STL template Efficiency

 

Generally, the STL template causesProgramThe running efficiency slows down because some standard data structures in the STL template need to allocate a large amount of memory space and context switching overhead. Sometimes containers such as vector are running, in case of insufficient capacity, it is very time-consuming to repeatedly allocate larger memory and copy the original content.

In addition, if you use the STL templateMake sure that a data structure is shared globally.Otherwise, the memory will be re-allocated each time a local data structure is defined, which is very time-consuming.

This is essentially the concept. Try to apply for a good memory space before running the program. do not apply continuously during the program running. In this way, the system requires constant context switching overhead.In addition, for containers that dynamically allocate storage space like a vector, if the problem permits the amount, the space size is predefined directly..

 

2. input and output efficiency

 

When there is a large number of input and output, do not use stream input and output, otherwise the program execution speed is very restrained.
You can use the input and output operations in C language. (I compared the two Input and Output Modes in a program with large input and output, and found that using the C language output input function increases the program execution speed by nearly 800 ms)

 

If you have time, sort it out:

 

 

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.