Code1
Code2
These code seem not to be differ too much but execution time have much differ. Example in case n = 500, code1 run in 3103ms but code 2 run only 789ms. What is the reason ? Pls explain to me. Thanks.
# | User | Rating |
---|---|---|
1 | jiangly | 3898 |
2 | tourist | 3840 |
3 | orzdevinwang | 3706 |
4 | ksun48 | 3691 |
5 | jqdai0815 | 3682 |
6 | ecnerwala | 3525 |
7 | gamegame | 3477 |
8 | Benq | 3468 |
9 | Ormlis | 3381 |
10 | maroonrk | 3379 |
# | User | Contrib. |
---|---|---|
1 | cry | 168 |
2 | -is-this-fft- | 165 |
3 | Dominater069 | 161 |
4 | Um_nik | 160 |
5 | atcoder_official | 159 |
6 | djm03178 | 157 |
7 | adamant | 153 |
8 | luogu_official | 150 |
9 | awoo | 149 |
10 | TheScrasse | 146 |
Why does loop order affect too much to execution time ?
ll res = -oo; for(int k=1; k<=n; k++) for(int i=1; i<=n-k+1; i++) for(int j=1; j<=n-k+1; j++) { f[i][j][k] = f[i][j][k — 1] + a[i + k — 1] * b[j + k — 1]; res = max(res, f[i][j][k]); } cout << res;
ll res = -oo; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) for(int k=1; k<=n; k++) { if(k + i > n || k + j > n) break; f[i][j][k] = f[i][j][k — 1] + a[i + k — 1] * b[j + k — 1]; res = max(res, f[i][j][k]); } cout << res;
These code seem not to be differ too much but execution time have much differ. Example in case n = 500, code1 run in 3103ms but code 2 run only 789ms. What is the reason ? Pls explain to me. Thanks.
Name |
---|