This article discusses the design and optimization of algorithms from the perspective of computer structures and shares them with you.
In algorithm design, we generally consider an algorithm theoretically, that is, how to optimize the time-space complexity. However, it is not easy to implement an algorithm perfectly. So the theory
Do not consider implementing algorithms as easy, because your algorithms may not have an effective implementation method in practice, and even if they can be easily implemented, however, the implementation personnel do not know about computers.
The structure of the algorithm is not satisfactory.
For example, the following program:
01 # include <stdio. h>
02 # include <time. h>
03 # define n 10000
04 # define M 10000
05
06 int sumrow (INT (* A) [m], int N, int m)
07 {
08 int sum = 0, I, J;
09 for (I = 0; I <n; ++ I)
10 For (j = 0; j <m; ++ J)
11 sum + = A [I] [J];
ReturnSUM;
12}
13
14 int sumcol (INT (* A) [m], int N, int m)
15 {
16 int sum = 0, I, J;
17 For (I = 0; I <m; ++ I)
18 For (j = 0; j <n; ++ J)
19 sum + = A [J] [I];
ReturnSUM;
20}
21
22 int a [n] [m];
23
24 int main ()
25 {
26 int pre, NEX, sum, I, J;
27
28 For (I = 0; I <n; ++ I)
29 for (j = 0; j <m; ++ J)
30 A [I] [J] = 1;
31
32 pre = clock ();
33
34 // sum = sumrow (A, n, m );
35
36 sum = sumcol (A, n, m );
37
38 NEX = clock ();
39
40 printf ("% DMS \ n", NEX-pre); // time statistics
41
42 getchar ();
43
44 return 0;
45}
My computer configuration: Windows XP, amd turion 64X2, 1.9 GHz, GB memory
Sumrow is about 800 ms, while sumcol is 11300 ms, with a difference of about 13 times. You can think about why?
Let me talk about it.
This is related to the storage structure of the computer. In order for the memory to keep up with the processing speed of the CPU, the high-speed cache is specially added between the CPU and the memory. High-speed cache access speed can be considered as close to the CPU frequency
Rate, so if the data is put in it, our program will be faster, otherwise it will be several times or even dozens of times slower. However, this high-speed cache capacity is limited, so the computer speculate what should be put inside to improve
Hit rate. In our program, we can find that the computer considers a small part of the continuous unit after the access unit as a very likely access unit, and puts it into the cache, in C, two-dimensional arrays are stored by row.
. The cache size is 1 MB. A maximum of 262144 integers can be placed at a time. A total of 381 hits occur for 10 ^ 8 rows of data. For column-based access, the 262144 elements can only cover each
The 26 rows of the column. Therefore, when each column is accessed, there is one miss for every 27 elements, so there are 10000 hits for 371 rows, therefore, in the worst case, access to these 10 ^ 8-power elements will not hit
3710000 times. Of course, the calculation is not accurate, but it is enough to explain the problem: sumcol does not have a good spatial locality, and the efficiency difference is 10 times. So sometimes it is possible to optimize the code implementation.
Optimization Algorithms are more efficient.
If you want to reach this level, you have to touch the computer's temper. It is just fun that the theory is absolutely unwilling to look at the computer structure, rather than the algorithm for system development. Institute
It is difficult to perfect an algorithm.
Here, I just want to say that, when optimizing program efficiency, if you cannot find a good solution theoretically, you may wish to improve the computer architecture, after all, your program runs on a computer.
Address: http://www.cnblogs.com/haolujun/archive/2011/06/13/2079555.html