C ++ performance optimization practices

Source: Internet
Author: User



1 Gprof





Practice

1. Time consumed for initializing large objects

The entire execution process is called 307 times, and the initialization time of the object accounts for 6.5%.
Before the program enters the constructor, the parent class Object of the class and all child member variable objects have been generated and constructed. It is a waste to assign values to constructors. If the constructor already knows how to initialize the sub-member variables of the class, you should assign the initialization information to the sub-member variables through the initialization list of the constructor, instead of performing the initialization in the constructor body. Because these sub-member variables have been initialized once before they enter the constructor. In the C ++ program, creating/destroying objects is a very prominent operation that affects performance. First, if an object is generated from the global heap, you must first perform the dynamic memory allocation operation. As we all know, dynamic allocation/recycling has always been very time-consuming in C/C ++ programs. Because it involves finding memory blocks with matching sizes, you may need to truncate them after finding them, and then you need to modify and maintain the linked list of global heap memory usage information.

2. Improper Map use






    string Recordset::GetField(const string &strName){    int nIndex;    if (hasIndex==false)    {        nIndex = m_nPos;    }    else    {        nIndex = m_vSort[m_nPos].m_iorder;    }    if (m_fields[strName]==0)    {        LOG_ERR("Recordset::GetField:"<<strName<<" Not Find!!");    }    return m_records[nIndex].GetValue(m_fields[strName] - 1) ;}

    string Recordset::GetField(const string &strName){    unordered_map::iterator iter = m_fields.find(strName);    if (iter == m_fields.end())    {        LOG_ERR("[Recordset::GetField] "<< strName <second - 1) ;}








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.