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.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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.
Rev. | Язык | Кто | Когда | Δ | Комментарий | |
---|---|---|---|---|---|---|
en3 | ExpectoPatronum | 2021-09-26 18:57:28 | 48 | |||
en2 | ExpectoPatronum | 2021-09-26 18:56:10 | 101 | Tiny change: ' << res;\n\n\n\n\n~~' -> ' << res;\n~~~~~\n\n\n\n~~' | ||
en1 | ExpectoPatronum | 2021-09-26 09:27:41 | 997 | Initial revision (published) |
Название |
---|