今天看C编程指南里说道:
1)在多重循环中,应当将最长的循环放在最内层,最短的循环放在最外层,以减少CPU跨循环层执行的次数。参考代码如下:
1
//
低效率:长循环在最外层
2 for (row = 0 ; row < 100; row + + )
3 {
4 for ( col = 0 ; col < 5; col + + )
5 {
6 sum = sum + a[row][col];
7 }
8 }
2 for (row = 0 ; row < 100; row + + )
3 {
4 for ( col = 0 ; col < 5; col + + )
5 {
6 sum = sum + a[row][col];
7 }
8 }
1
//
高效率:长循环在最内层
2 for (col = 0 ; col < 5; col + + )
3 {
4 for (row = 0 ;
2 for (col = 0 ; col < 5; col + + )
3 {
4 for (row = 0 ;