Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •