Author |
Message |
viza #1 / 11
|
 if/else vs. ternery efficiency
Hi! Which of the following is more efficient? $var=a?b:c?d:e?f:g?h:i; if(a){ $var=b; }elseif(c){ $var=d; }elseif(e){ $var=f; }elseif(g){ $var=h; }else{ $var=i; } Thanks! In general, how do you tell which of two methods is better?
|
Mon, 25 Apr 2005 21:17:44 GMT |
|
 |
André N?s #2 / 11
|
 if/else vs. ternery efficiency
Quote:
> Hi! > Which of the following is more efficient? > $var=a?b:c?d:e?f:g?h:i; > if(a){ > $var=b; > }elseif(c){ > $var=d; > }elseif(e){ > $var=f; > }elseif(g){ > $var=h; > }else{ > $var=i; > } > Thanks! > In general, how do you tell which of two methods is better?
Ok, the first statement shouldn't even be considered because it's completely unreadable. Also, since the two statements do pretty much the same thing there is no reason to expect there to be any difference now is there? And finally, the efficiency of an if-statement won't affect the efficiency of your application, so why bother? As for your final question, in general I only optimize if needed, that is if something seems to slow I optimize it. IMO that is the optimal way :) Andr N?ss
|
Mon, 25 Apr 2005 23:14:09 GMT |
|
 |
Justin Koivist #3 / 11
|
 if/else vs. ternery efficiency
Quote:
> Hi! > Which of the following is more efficient? > $var=a?b:c?d:e?f:g?h:i; > if(a){ > $var=b; > }elseif(c){ > $var=d; > }elseif(e){ > $var=f; > }elseif(g){ > $var=h; > }else{ > $var=i; > } > Thanks! > In general, how do you tell which of two methods is better?
Define efficiency... 1. how long it takes your code to run? I don't think either would make a difference. 2. how long it takes you to debug the code? using the if/elseif would be _much_ more efficient since you can quickly see what it does without translation. --
PHP POSTERS: Please use comp.lang.php for PHP related questions, alt.php* groups are not recommended.
|
Mon, 25 Apr 2005 23:24:01 GMT |
|
 |
Sundial Service #4 / 11
|
 if/else vs. ternery efficiency
There's a great book, /The/ /Elements/ /of/ /Programming/ /Style,/ which makes some pretty important points such as: * Don't diddle the code to make it faster; find a better algorithm. * Make it clear. Above all else, I think, "make it /clear/ " and let the compiler or interpreter or whatever do its job. * At 100 million ops per second, no one can hear you scream. * One programmer still costs $75,000 a year. Quote:
> Which of the following is more efficient? > $var=a?b:c?d:e?f:g?h:i; > if(a){ > $var=b; > }elseif(c){ > $var=d; > }elseif(e){ > $var=f; > }elseif(g){ > $var=h; > }else{ > $var=i; > } > Thanks! > In general, how do you tell which of two methods is better?
|
Tue, 26 Apr 2005 00:09:36 GMT |
|
 |
stephan bea #5 / 11
|
 if/else vs. ternery efficiency
Quote:
> Above all else, I think, "make it /clear/ " and let the compiler or > interpreter or whatever do its job. > * At 100 million ops per second, no one can hear you scream. > * One programmer still costs $75,000 a year.
Excellent points which people often ignore. Another way of puting it: We often forget that program efficiency also includes the developer's time. -- ----- stephan beal Registered Linux User #71917 http://counter.li.org I speak for myself, not my employer. Contents may be hot. Slippery when wet. Reading disclaimers makes you go blind. Writing them is worse. You have been Warned.
|
Tue, 26 Apr 2005 00:39:53 GMT |
|
 |
Nikolai Chuvakh #6 / 11
|
 if/else vs. ternery efficiency
Quote: > Which of the following is more efficient?
In terms of speed of execution, they are probably the same. In terms of code readability, both are ugly, each in its own way. Quote: > $var=a?b:c?d:e?f:g?h:i; > if(a){ > $var=b; > }elseif(c){ > $var=d; > }elseif(e){ > $var=f; > }elseif(g){ > $var=h; > }else{ > $var=i; > } > In general, how do you tell which of two methods is better?
Neither. I'd use a switch {} instead... swtitch { case a: $var=b; break; case c: $var=d; break; case e: $var=f; break; case g: $var=h; break; default: $var=i; Quote: }
Cheers, NC
|
Tue, 26 Apr 2005 06:28:23 GMT |
|
 |
viza #7 / 11
|
 if/else vs. ternery efficiency
and then stephan beal said: Quote:
>> Above all else, I think, "make it /clear/ " and let the compiler or >> interpreter or whatever do its job. >> * At 100 million ops per second, no one can hear you scream. >> * One programmer still costs $75,000 a year. > Excellent points which people often ignore. Another way of puting it: > We often forget that program efficiency also includes the developer's > time.
Seeing's as I'm a hobbyist, (or at least, a willing volunteer), these don't really apply. I asked because if the single line one is quicker, then I would prefer it. I don't do this as a day job, and seem to be able to remember the purpose each line of code I have ever written as soon as I look at it - no one else is ever going to be looking at my code and I want to tweak it to death. Thanks for the advice! viza
|
Tue, 26 Apr 2005 07:12:15 GMT |
|
 |
viza #8 / 11
|
 if/else vs. ternery efficiency
and then Justin Koivisto said: Quote:
>> Which of the following is more efficient? >> $var=a?b:c?d:e?f:g?h:i; >> if(a){ >> $var=b; >> }elseif(c){ >> $var=d; >> }elseif(e){ >> $var=f; >> }elseif(g){ >> $var=h; >> }else{ >> $var=i; >> } > Define efficiency... > 1. how long it takes your code to run? I don't think either would make a > difference.
ok Quote: > 2. how long it takes you to debug the code? using the if/elseif would be > _much_ more efficient since you can quickly see what it does without > translation.
I would say anything that keeps more of the script on the page would help me work faster. If I write something I would hope that I could understand it at a glance without too much thought. The actual code looks like: $tnumber is a positive integer, I want a four digit string. if($tnumber<10){ $tnumber='000'.$tnumber; Quote: }elseif($tnumber<100){
$tnumber='00'.$tnumber; Quote: }elseif($tnumber<1000){
$tnumber='0'.$tnumber; Quote: }
You can do similar with str_pad(), I know, but guessed the above might be more effecient.
|
Tue, 26 Apr 2005 07:19:15 GMT |
|
 |
Steven Vasilogiani #9 / 11
|
 if/else vs. ternery efficiency
says... Quote: > I would say anything that keeps more of the script on the page would help me > work faster.
Sort of like using str_pad() or printf() rather than the kludgery which follows? ;-) Quote: > $tnumber is a positive integer, I want a four digit string. > if($tnumber<10){ > $tnumber='000'.$tnumber; > }elseif($tnumber<100){ > $tnumber='00'.$tnumber; > }elseif($tnumber<1000){ > $tnumber='0'.$tnumber; > } > You can do similar with str_pad(), I know, but guessed the above might be > more efficient.
The best way to know which code is more efficient is to benchmark it: <http://pear.php.net/package-info.php?pacid=53>. printf() or str_pad() are probably the best solution and I would stick with them regardless of whether or not they were more efficient (the difference would certainly be negligible). Still, I wouldn't mind seeing some of the results you come up with if you choose to do some benchmarking :-) HTH, -- steven vasilogianis
|
Tue, 26 Apr 2005 11:10:46 GMT |
|
 |
Justin Koivist #10 / 11
|
 if/else vs. ternery efficiency
Quote:
> and then Justin Koivisto said:
> >>Which of the following is more efficient? > >> $var=a?b:c?d:e?f:g?h:i; > >> if(a){ > >> $var=b; > >> }elseif(c){ > >> $var=d; > >> }elseif(e){ > >> $var=f; > >> }elseif(g){ > >> $var=h; > >> }else{ > >> $var=i; > >> } > >Define efficiency... > >1. how long it takes your code to run? I don't think either would make a > >difference. > ok > >2. how long it takes you to debug the code? using the if/elseif would be > >_much_ more efficient since you can quickly see what it does without > >translation. > I would say anything that keeps more of the script on the page would > help me > work faster. If I write something I would hope that I could > understand it > at a glance without too much thought. The actual code looks like: > $tnumber is a positive integer, I want a four digit string. > if($tnumber<10){ > $tnumber='000'.$tnumber; > }elseif($tnumber<100){ > $tnumber='00'.$tnumber; > }elseif($tnumber<1000){ > $tnumber='0'.$tnumber; > }
you could also use something like: $tnumber=sprintf('%04d',$tnumber); I don't know the specifics for performance with str_pad(), sprintf()... --
PHP POSTERS: Please use comp.lang.php for PHP related questions, alt.php* groups are not recommended.
|
Wed, 27 Apr 2005 04:18:48 GMT |
|
 |
monoli #11 / 11
|
 if/else vs. ternery efficiency
Quote: > Neither. I'd use a switch {} instead... > swtitch { > case a: > $var=b; > break; > case c: > $var=d; > break; > case e: > $var=f; > break; > case g: > $var=h; > break; > default: > $var=i; > } > Cheers, > NC
I agree, the "switch" is more readable. If you want, you could also think of it as being more efficient. Just say to yourself that after finding a case that matches, the switch statement ends. :)
|
Fri, 13 May 2005 13:12:22 GMT |
|
|