Блог пользователя guess_who

Автор guess_who, история, 3 года назад, По-английски

Hello guys!

I was solving some problems on dp on subsets and I found this strange one

this got AC, but that gets TLE, and the only difference between them is swaping the inner 2 loops (mask and j).

I'm really curious to know why :D

  • Проголосовать: нравится
  • +13
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

I guess it's a compiler optimization

In your code if $$$g[i][j]$$$ is false, it won't do anything in the loop.

I guess GCC adds if (!g[i][j]) break; to the end of line 19

»
3 года назад, скрыть # |
 
Проголосовать: нравится +36 Проголосовать: не нравится

Second code gets TLE'd because of some cache shenanigans.

My thoughts that CPU in first submit prefetches full cache line, so access to $$$newdp[mask]$$$ is almost free, but in the other submit, it can be washed out by accessing $$$g[i][j]$$$.

And AFAIK, arrays with len that are powers of two are quite bad for performance reasons on some CPUs, see this link for reference.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +18 Проголосовать: не нравится

similar thing has happened with me once on cf. i think it is due to more cache miss rate in second one but can't guess why it's happening