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 5:59

depends on a

Name: Anonymous 2010-07-13 7:37

depends on [k]a[/k]nus

Name: Anonymous 2010-07-13 7:43

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

Name: Anonymous 2010-07-13 7:45

depends on [size=32]anus[/size]

Name: Anonymous 2010-07-13 7:58

The first one is probably faster overall because when a <= 3 it won't perform most of the operations.

Name: Anonymous 2010-07-13 8:25

well here are my results

[table]
[tr][th][/th][th]code1[/th][th]code2[/th][/tr]
[tr][th]1st pass[/th][th]0.3583[/th][th]0.3522[/th][/tr]
[tr][th]2nd pass[/th][th]0.3533[/th][th]0.3542[/th][/tr]
[tr][th]3rd pass[/th][th]0.3535[/th][th]0.3525[/th][/tr]
[/table]

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.

Name: Anonymous 2010-07-13 12:24

>>3,5,7

SO MUCH BBCODE FAILURE!

Name: Anonymous 2010-07-13 14:21

Could someone please ask that on stackoverflow?

Name: >>10 2010-07-13 15:10

Please? I'm banned from there after I answered every question with U MENA HASkAL.

Name: Anonymous 2010-07-13 15:13

>>11
U MENA Fuck right off

Name: Anonymous 2010-07-13 15:54

>>10-11
so that's where you came from. go back there, please.

Name: Anonymous 2010-07-13 16:32

>>12
Way to crush his hopes and dreams dude. No, really!

Name: Anonymous 2010-07-13 19:45

>>6
We don't know what a is, it could be 2 99% of the time.

Name: Anonymous 2010-07-13 20:20

The first one will be faster since there is one less call to make for a<=3. Even that's the case only once, it'd be enough.

Have some pseudo-assembly:
  cmp a,3
  jle cont
;rest
cont:

vs.
  call fun
  mov b, result
  cmp a,3
  jle cont
;rest
cont:

Name: Anonymous 2010-07-13 20:32

>>16
see >>8.

Name: Anonymous 2010-07-14 3:33

I WOULD LIKE TO HEAR FV'S OPINION ON THIS.

Name: Anonymous 2010-07-14 3:46

>>18
I'm sure you would. Now get the fuck out and stop spamming.

Name: FrozenVoid 2010-07-14 4:36

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


__________________
Orbis terrarum delenda est

Name: Anonymous 2010-07-14 4:46

>>20
FV copying a previous post in the thread with a couple minor changes?
either he's getting lazy, or this is an imposter.

Name: Anonymous 2010-07-14 7:08

>>21
Have you not been paying attention the past couple weeks?  It is obviously an impostor.

Name: Anonymous 2010-07-14 7:36

>>22
??

Name: Anonymous 2010-07-14 8:57

>>23
Read some of his posts. Most of them are not at all FV-quality. A few are pretty damn close, however.

Name: Anonymous 2010-07-14 13:58

>>18,20-24
No one cares. Not one single person.

Name: Anonymous 2010-07-14 14:16

>>25
I care. >>21 seems to care. >>22 cares enough to have noticed. >>18 cares. You seem to care too, if only in meta.

Name: Anonymous 2010-07-14 15:43

>>26
The metaplane is a plane of opposites.

Name: Anonymous 2010-07-14 22:04

The first one is the one that could be faster but since there's only one line after the if-statement, I would remove the braces if you're going to be looping through that if-statement any large number of times because the braces will slow down the compiler each time you run through it.

Name: Anonymous 2010-07-14 22:39

>>27
#Did you mean: The /prog/scape is a vista of bullshit

Name: Anonymous 2010-07-15 1:20

>>27
This reminds me of how the integral of a sine is minus cosine. Just like that.

Name: Anonymous 2010-07-15 3:41

Someone please post those EXPERT C PROGRAMMING tips.

Name: FrozenVoid 2010-07-15 3:56

Name: Anonymous 2010-07-15 4:23

>>28
Poor compiler being slowed down. The has a really big effect on the program in the end, because if something is compiled slowly, it can only be executed slowly.

Name: Anonymous 2010-07-15 18:22

>>33
I know this all too well. I had a lot of problems with this stuff when I was in high school.

Name: Anonymous 2010-07-15 18:25

>>34
You were conceived over the course of 4 hours and find the most menial tasks incredibly difficult.

Name: Anonymous 2010-07-15 19:21

[/CODE]

Name: Anonymous 2010-07-15 19:27

"

Name: Anonymous 2010-11-15 10:37

Name: Anonymous 2011-02-04 19:37


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