C ++ performance optimization memo

Source: Internet
Author: User
= Common Factors Affecting Performance =
* Access/read/write slow disks, network devices, or external devices
* Frequent New/delete objects, especially complex large objects
* Frequent function calls also bring performance overhead.
* Frequent process/thread creation and database/network connections * inappropriate data structures and inefficient Algorithms

= Solutions to these factors =
* Use the internal cache to reduce the number of times the peripherals are read, and the operations on the Read and Write peripherals are placed in a separate thread.
* Use a memory pool or other memory optimization policies, and use as many references or pointers as possible to reduce the generation of temporary objects.
* You can use inline functions or macros to improve performance and limit unnecessary virtual functions.
* Process pool, thread pool, and connection pool
* Suitable data structures and algorithms are used for different application scenarios.

 

= Theoretical documents =

Valid C ++

Effcient C ++

The art of computer programming

 

= Practice =
* Identify performance bottlenecks
* Obtain performance data through tests.
* Constantly modify, test, observe, and analyze data

// As the below example, we can find
// ++ I Is Better Than I ++
// A + = B is better than a = a + B

 

Class
Cproduct

...
{

Friend
Const
Cproduct
Operator
+
(
Const
Cproduct
&
LHS,
Const
Cproduct
&
RHS );

Public
:

Cproduct (
Int
Price );

Cproduct (
Const
Cproduct
&
RHS );


Virtual
 
~
Cproduct ();


Operator
=
(
Const
Cproduct
&
RHS );



Cproduct
&
 
Operator
=
(
Const
Cproduct
&
RHS );

Cproduct
&
 
Operator
+ =
(
Const
Cproduct
&
RHS );

Cproduct
&
 
Operator
++
();

Cproduct
Operator
++
(
Int
);

Private
:


Int
M_nprice;


}

;



Cproduct: cproduct (
Int
Price): m_nprice (price)

...
{


}




Cproduct ::
~
Cproduct ()

...
{


}




Cproduct: cproduct (
Const
Cproduct
&
RHS): m_nprice (RHS. m_nprice)

...
{


}




Cproduct
&
Cproduct ::
Operator
=
(
Const
Cproduct
&
RHS)

...
{


If
(
This
== &
RHS)


Return
 
*
This
;

M_nprice
=
RHS. m_nprice;


Return
 
*
This
;

}




Const
Cproduct
Operator
+
(
Const
Cproduct
&
LHS,
Const
Cproduct
&
RHS)

...
{


Return
Cproduct (LHS. m_nprice
+
RHS. m_nprice );

}




Cproduct
&
Cproduct ::
Operator
+ =
(
Const
Cproduct
&
RHS)

...
{

M_nprice
+ =
RHS. m_nprice;


Return
 
*
This
;

}




Cproduct
&
Cproduct ::
Operator
++
()
//
Prefix


...
{


++
M_nprice;


Return
 
*
This
;

}




Cproduct ::
Operator
++
(
Int
)

...
{

Cproduct TMP (
*
This
);


++
(
*
This
);


Return
TMP;

}



Refer

 

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.