C Program Optimization Road
This article tells the common optimization method for writing C program code, divided into I / O article, memory articles, algorithms, MMX assembly articles.
One. I / O
If there is a file read or written, the access to the file will be a major factor affecting the speed of the program. There are two main ways to improve file access speed: First, use memory mapping files, and the other is to use memory buffers. Below is a set of test data (see "Advanced Programming" section of Unix Environment), which shows the results obtained with 18 different cache lengths, read 1 468 802 byte files.
Buffer size
User CPU (second)
System CPU (second)
Clock time (seconds)
Number of cycles (seconds)
1
23.8
397.9
423.4
1 468 802
2
12.3
202.0
215.2
734 401
4
6.1
100.6
107.2
367 201
8
3.0
50.7
54.0
183 601
16
1.5
25.3
27.0
91 801
32
0.7
12.8
13.7
45 901
64
0.3
6.6
7.0
22 951
128
0.2
3.3
3.6
11 476
256
0.1
1.8
1.9
5 738
512
0.0
1.0
1.1
2 869
1 024
0.0 0.6
0.6
1 435
2 048
0.0 0.4
0.4
718
4 096
0.0 0.4
0.4
359
8 192
0.0 0.3 0.3 180
16 384
0.0 0.3 0.3 90
32 768
0.0 0.3 0.3 45
65 536
0.0 0.3 0.3 23
131 072
0.0 0.3 0.3 12
It can be seen that when the memory buffer size is 8192, performance is already the best, which is why the buffer size is 8192 in an image encoding program (sometimes taken) in an image encoding program such as H.263. 2048 size). The advantage of using a memory buffer method is mainly for porting, accounting for less memory, which is convenient for hardware implementation. Below is a C-pseudo code reading files:
Int Len;
BYTE BUFFER [8192];
Assert (buffer == null);
If Buffer Is Empty {
Len = Read (File, Buffer, 8192);
IF (len == 0) no data and exit;
}
However, if the memory is relatively large, the memory map file can achieve better performance, and the programming is simple. Conventional instructions for memory maps See Platform SDK in MSDN October 2001
Documentation-Base Services-File Storage-file mapping. Here is a suggestion:
1 The memory map file cannot exceed the size of the virtual memory. It is best not to be too large. If the memory map file is close to the virtual memory size, it will greatly reduce the speed of the program (in fact, because the virtual memory is not enough, the system running efficiency is reduced), At this time, you can consider the block mapping, but I think if so, it is better to use memory cockroaches directly.
2 can be used in uniform use, such as when I am marking the image file data processing (because it is a UNIX workstation, the memory GB unit) uses the memory map file, but for the best performance, a line of image cache is used. In this way, when the data is read, it is guaranteed that it is just sequential read and write (memory mapping files, which is readwritten in order). 3 Use memory mapping files when writing files: You should create a big enough file, then map this file, and use the function setFilePointer and setndoffile to cut the file when processing this file.
4 Operation of the memory mapped file is similar to the memory (like the image array), then remember to use the memcpy () function if there is a large block of data read and write, remember to use the memcpy () function (or copymemory () function)
In summary, if you want to use a memory map file, you must: 1. Processing files, 2. Processing files are large, but the operating environment is also very large, and generally do not run other consumption memory when running the program The program, while users have special requirements for speeds, and there is no requirement for memory. If the above two conditions are not satisfied, it is recommended to use a memory buffer.