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

Автор johnchen902, история, 8 лет назад, По-английски

Today I learned that NaNs and infinites may slow down programs significantly. So instead of

// some computation that produce NaN when arg == 0
if(arg == 0) // special case
    result = 0;

one should write

if(arg == 0)
    result = 0; // special case
else
    // some computation that produce NaN when arg == 0

Compare submission 19257027 and 19257693 and you'll see.

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

»
8 лет назад, # |
Rev. 2   Проголосовать: нравится +20 Проголосовать: не нравится

Instructions involving denormalized(very small) values are also very slow.

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится +33 Проголосовать: не нравится

    I've heard of a trick to optimize the DP (sometimes up to tens times!):

    d[i][j] = ... (compute d[i][j] here)
    if (d[i][j] < 1e-18) d[i][j] = 0; // this is the trick
    

    After this optimization no computations are made with very small numbers, so denormalized numbers are not involved too, so it causes a speedup.

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I've never experimented with denormals, but supposedly you can turn them off by calling _controlfp_s with the _DN_FLUSH flag set.

»
8 лет назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

After reading this blog, I realized...

Thanks for the heads-up :) I guess I got really lucky today. (After fixing issue you described the time became 140ms)