Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[C] Which is faster?

Name: Anonymous 2010-07-13 5:57


if(a > 3)
{
  int expected_b = fun(a);
  if(good_enough(b, expected_b))
  {
    do_stuff();
  }
}


VS


int expected_b = fun(a);
if(a > 3 && good_enough(b, expected_b))
{
  do_stuff();
}


This is done a shitload of times. fun(a) is a simple linear function.

Name: Anonymous 2010-07-13 8:30

>>1
if fun doesn't have any side effects, they are equivalent and should be compiled to the same code. if fun does have side effects, it doesn't matter which is faster, you have to choose which one to do based on whether you always need those side effects or only need them when a > 3.
you might be able to do better, tho, if you don't need to use expected_b later and either fun or good_enough doesn't have side effects...
if only good_enough has side effects:
if(a > 3 && good_enough(b, fun(a)) do_stuff();
if only fun has side effects:
if(good_enough(b, fun(a)) && a < 3) do_stuff();
if both of them have side effects, you might be able to do better or you might not, depending on exactly what it is you're trying to do.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List