Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2010, 06:05 PM   PM User | #1
sitNsmile
Regular Coder

 
sitNsmile's Avatar
 
Join Date: Dec 2009
Location: Charlotte, NC
Posts: 354
Thanks: 19
Thanked 2 Times in 2 Posts
sitNsmile is an unknown quantity at this point
if statment or Conditional OP faster?

Okay, I know the mark must be extremely small to test, but I notice the "Conditional operator" slightly use less php memory.

which here is my test;


PHP Code:
if($orderType=='DESC'){$orderType='DESC';}
elseif(
$orderType=='ASC'){$orderType='ASC';}
else{
$orderType='ASC';} 
or (Conditional operator)

PHP Code:
$orderType $oType == 'DESC' 'DESC' 'ASC' 
Same type of idea, just using a different method. but as building larger application, speed and resources are critical.


also just realized could use the "switch/case" in this test as well.

PHP Code:
switch($oType)
{
case 
'DESC':$orderType 'DESC';break;
case 
'ASC':$orderType 'ASC';break;

Which you rather use? if statement yes is easier to read.

Last edited by sitNsmile; 04-01-2010 at 06:09 PM..
sitNsmile is offline   Reply With Quote
Old 04-01-2010, 06:14 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The conditional expression versus the ternary is debatable. This is one of those ones where you shouldn't be able to notice a difference in either case.
If you have a single if/else (which that should be, there is no need for an else if in there), the Ternary will be faster. However, it is more difficult to add additional checks, since you need to chain the ternary: (conditional) ? ((conditional) ? true : false) : ((conditional) ? true : false) so it becomes difficult to read.
The best choice if expansion is possible is to use a switch which is faster than an if/else check. This is especially true if you want to allow multiple if's to result in the same result, but is not useful if you're using datatype checking since it uses loose comparisons (== and never === checks). Unlike an if/elseif/else pair, the switch can potentially use more memory and time if you never break.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 04-01-2010, 06:31 PM   PM User | #3
sitNsmile
Regular Coder

 
sitNsmile's Avatar
 
Join Date: Dec 2009
Location: Charlotte, NC
Posts: 354
Thanks: 19
Thanked 2 Times in 2 Posts
sitNsmile is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
The conditional expression versus the ternary is debatable. This is one of those ones where you shouldn't be able to notice a difference in either case.
If you have a single if/else (which that should be, there is no need for an else if in there), the Ternary will be faster. However, it is more difficult to add additional checks, since you need to chain the ternary: (conditional) ? ((conditional) ? true : false) : ((conditional) ? true : false) so it becomes difficult to read.
The best choice if expansion is possible is to use a switch which is faster than an if/else check. This is especially true if you want to allow multiple if's to result in the same result, but is not useful if you're using datatype checking since it uses loose comparisons (== and never === checks). Unlike an if/elseif/else pair, the switch can potentially use more memory and time if you never break.
Thanks yeah, makes a lot of sense. I used to use if/else so much, so working on moving into other methods, and tired of Procedural code, getting more into OOP with new projects.
sitNsmile is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 AM.


Advertisement
Log in to turn off these ads.