johnchen902's blog

By johnchen902, history, 8 years ago, In English

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.

  • Vote: I like it
  • +61
  • Vote: I do not like it

»
8 years ago, # |
Rev. 2   Vote: I like it +20 Vote: I do not like it

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

  • »
    »
    8 years ago, # ^ |
      Vote: I like it +33 Vote: I do not like it

    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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

»
8 years ago, # |
  Vote: I like it +15 Vote: I do not like it

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)