New Idea of Algorithm Design

Source: Internet
Author: User

AlgorithmDesign, we generally consider an algorithm from the theoretical perspective, that is, how to optimize the time-space complexity. However, it is not easy to implement an algorithm perfectly. Therefore, people engaged in theory should never regard implementation algorithms as easy tasks, because your algorithms may not have an effective implementation method in reality, and even if they can be easily implemented, however, because the implementers do not understand the structure of the computer, the algorithm is not satisfactory.

For exampleProgram: 

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 (*) [ 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. The high-speed cache access speed can be considered to be close to the CPU frequency. Therefore, if data is stored in the cache, our program will be faster; otherwise, it will be several times or even dozens of times slower. However, the cache capacity is limited, so the computer determines what to put in to increase the 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. Therefore, for column-based access, 262144 elements can only cover 26 rows in each column. Therefore, when accessing each column, each 27 elements will not hit each other, in this case, 10000 rows have a total of 371 hits, so in the worst case, access to the 10 ^ 8 power element won't 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 the Code implementation may be more efficient than the optimization algorithm theoretically. 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. Therefore, it is difficult to perfect an algorithm.

here, I just want to say that, when optimizing program efficiency, if theoretically there is no good solution, then we can improve the computer architecture. After all, your program runs on the computer.

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.