= 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