1. When analyzing an operation, you need to first classify it as IO intensive or CPU intensive.
This can be distinguished by using a virtual hard disk tool (such as ramdisk tool) to virtualize a portion of the memory into a hard disk. Applications and reference files are installed on the virtual hard disk. If the program performance is improved significantly, this is Io-related.
II. I/O bottleneck optimization:
Each Io operation is about 10 ms, and about 1 s for 100 times. Therefore, avoid unnecessary Io as much as possible. The specific practices are as follows:
Pre-sequential file read to avoid random access
Merge multiple small files into a single large file
Optimize loading of dynamic library files
The IO time and CPU time are staggered.
3. Intensive Computing Optimization
Computing-intensive performance problems include Memory Allocation performance, string operations, and mutex lock protection for shared variables. The specific optimization methods include:
Remove redundant code
String operation optimization
Reduce memory allocation and release operations, such as using a memory pool
Remove unnecessary mutex operations
Select a Data Structure Based on performance requirements
Latency and On-Demand execution. For example, when an image in a document is redirected to a page, it is loaded to the memory.
Reduce cross-process calls
Use high-performance function libraries
3-1-byte alignment (may vary with platforms and may change the settings)
In the Global static data area, the data is aligned by four bytes. In the heap, the data is aligned by 16 bytes.
2. string constants are stored in the global storage area. This area is allocated during the compilation phase and cannot be modified during program running.
Char * plocalstring1 = "localstring1"; // point to a long character constant
Plocalstring [1] = 'a'; // try to modify an unmodifiable memory zone.
This code will cause failure during running
3. memory units on the stack will be automatically released, but the memory on the stack will not be automatically released.
Int * pnew = new int [5];
Char * pmalloc = (char *) malloc (1 );
Pnew and pmalloc in the Code are on the stack and will be automatically released, but the memory they point to is on the stack and needs to be released through free () and delete.
In addition, the stack size is generally limited (1 MB by default on VC), so the following code will run an error.
Int main ()
{
Char Buf [1024*1024]; // declare a 1 MB array on the stack, Stack Overflow
Return 0;
}
Objects in 4 C ++ are an area in the memory.
If an object is defined as a global mainland variable, it is stored in the global/static data zone;
If an object is created by defining a variable in a function or implementing a required temporary variable, it is an object on the stack.
If an object is created through the new operator, it is an object on the stack.
Note that the scope in C ++ is defined by {And}, rather than the entire function.
Int Foo ()
{
Int nval = 0;
.................
If (nval> 0)
{A; // A is a class
// Object A is created when the condition statement is set up and destroyed when the condition statement is exited.
}
P22 illustrates how to create and destroy some hidden intermediate temporary variables when an object is created.
5. Copying Constructors
If the copy constructor is not defined in the class, the compiler will generate a default copy constructor. By default, the copy constructor performs a bit copy, that is, copying by byte according to the memory space of the object to be searched. This may cause implicit memory problems.
# Include <stdio. h>
Class simpleclass
{
PRIVATE:
// Simpleclass (simpleclass &);
Char * buffer;
Public:
Int nvalue;
Simpleclass (int n)
{Nvalue = N;
Buffer = new char [N];};
~ Simpleclass ()
{
If (buffer) {Delete buffer ;}
};
};
Int main ()
{
Simpleclass A (10 );
Simpleclass B =;
Return 0;
}
In this program, because the default copy constructor is used, the buffer of A and B points to the same memory zone. During the analysis, the program is released twice, causing program crash.