Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342

    Arrow Two situations, which is more efficent than the other?

    I have a data checking for a post, the post consitits of x number of fields..

    Is this better:

    PHP Code:
    <?php

    if($_POST[name] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name1] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name2] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name3] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name4] == NULL){$message 'Error';}

    ?>
    Then today I just thought to myself that this would be faster (since less conditions in the if statement), and it wont go through ALL if statements:

    PHP Code:
    <?
    while($x == NULL){
    if(
    $_POST[name] == NULL){$message 'Error';break;}
    if( 
    $_POST[name1] == NULL){$message 'Error';break;}
    if(
    $_POST[name2] == NULL){$message 'Error';break;}
    if(
    $_POST[name3] == NULL){$message 'Error';break;}
    if( 
    $_POST[name4] == NULL){$message 'Error';break;}
    break;
    }
    ?>
    am I right here?

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  2. #2
    Join Date
    Nov 2005
    Location
    Buffalo
    Posts
    94
    PHP Code:
    if( ($_POST[name] == NULL) || 
        (
    $_POST[name1] == NULL) ||
        (
    $_POST[name2] == NULL) ||
        (
    $_POST[name3] == NULL) ||
        (
    $_POST[name4] == NULL)) {$message 'Error';} 
    I believe PHP uses Short Circuit logic so first true in "or" conditional statemnet means the rest of the conditions don't get tested.

    I guess your probably right about the second being faster. How do you know $x is null, if registered globals are on then $x could be set by POST or GET.
    Malenski.com - "In Valid Code We Trust"

  3. Quote Originally Posted by azizny
    I have a data checking for a post, the post consitits of x number of fields..

    Is this better:

    PHP Code:
    <?php
     
    if($_POST[name] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name1] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name2] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name3] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name4] == NULL){$message 'Error';}
     
    ?>
    Then today I just thought to myself that this would be faster (since less conditions in the if statement), and it wont go through ALL if statements:

    PHP Code:
    <?
    while($x == NULL){
    if(
    $_POST[name] == NULL){$message 'Error';break;}
    if( 
    $_POST[name1] == NULL){$message 'Error';break;}
    if(
    $_POST[name2] == NULL){$message 'Error';break;}
    if(
    $_POST[name3] == NULL){$message 'Error';break;}
    if( 
    $_POST[name4] == NULL){$message 'Error';break;}
    break;
    }
    ?>
    am I right here?

    Peace,
    You definately have it right - the second one would work better.
    Quantum Net Solutions - www.quantumns.com

    We offer fast, reliable and good quality web hosting services. Visit us today - you won't be disappointed!

  4. #4
    Go the second one. More efficient.
    Revolutionary Host! Revolutionizing the way you look at hosting!
    Revolutionary Host

  5. #5
    Join Date
    Apr 2003
    Location
    Los Angeles, CA
    Posts
    800
    Neither one is great, IMO. If you have different error messages, then use something like this for a cleaner way of doing it (especially for many fields). I doubt it's slower than the other suggestions but you could benchmark it .

    PHP Code:
    $errors = array(
      
    'name1' => 'First Error',
      
    'name2' => 'Other Error',
      
    'name3' => 'Another Error'
    );

    foreach(
    $errors as $f => $m) {
      if(
    $_POST[$f] == NULL) {
        
    $message $m;
        break;
      }

    Pings <1 ms, Unlimited Transfer, Lowest Price: http://localhost/

  6. #6
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    I'd use a switch and then store the errors in an array.

    $error[] = "Bla, your names too long";

    then you can use count($error) to determine how many errors there are...if there are.

    if (count($error)<1) // No error...continue.
    WLKNS.co - A collection of my programmer thoughts

  7. #7
    I'd construct the form fields in this manner:

    <input type="text" name="names[1]" />
    <input type="text" name="names[2]" />

    That way you can have multiple input fields which will turn into an array after beeing submitted.

    Then, you can combine what arkin said - like this:

    PHP Code:

    if(is_array($_POST['names']))
    {
        foreach(
    $_POST['names'] as $dummy => $value)
        {
            if(empty(
    $value)) $errors[] = 'Please fill in all required data';
        }
        
        if(
    sizeof($errors))
        {
            
    // no empty fields, proceed with further processing
        
    }
        else
        {
            
    // empty fields, report errors and display the form again
        
    }

    I didn't test the example, so don't just copy paste it

    Also, $_POST[name] differs from $_POST['name'] so quote your array indexes if you're using associative arrays!

  8. #8
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,342
    Some points:

    * I have register globals on.

    * The issue of Arrays is out of question for me:
    - I always use constructive names ("user_email","user_address"), and also I have many lists which do not need checking so why loop to them to?

    Thanks alot for your helpful ideas and answers,

    Peace,
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  9. #9
    Join Date
    May 2004
    Location
    NYC
    Posts
    793
    malenski posted the best solution. And he's right; the conditional will kick out as soon as it fails (if it fails at all). Just make sure to set $message='' first!

  10. #10
    Join Date
    Oct 2004
    Posts
    104
    OMG. Stop thinking about this. It makes no real world difference.

  11. #11
    OMG. Stop thinking about this. It makes no real world difference.
    hehe. quoted for truth.
    helps pass the time though...


    PHP Code:
    <? for ($i=0$i<4$i++) 
        if (
    $_POST['name'.i] == NULL) {
            
    $message 'Error'
            break; } 
    ?>

  12. #12
    Join Date
    Nov 2005
    Posts
    268
    I did some testing on your suggestions and some of my own, and I found that the IF statements themselves are MUCH faster.

    PHP Code:
    <?php
    function microtime_float()
    {
       list(
    $usec$sec) = explode(" "microtime());
       return ((float)
    $usec + (float)$sec);
    }
    //which is the fastest way to do a break comparison.
    //model 0
    $_POST[name] = "george";
    $_POST[name1] = "george";
    $_POST[name2] = "george";
    $_POST[name3] = "george";
    $_POST[name4] = NULL;
    $x == NULL;

    $iii=0;
    $ii=100000;
    $start[0] = microtime_float();

    for(
    $i=0;$i<$ii;$i++)
    {
        while(
    $x == NULL){
        if(
    $_POST[name] == NULL){$message 'Error';break;}
        if( 
    $_POST[name1] == NULL){$message 'Error';break;}
        if(
    $_POST[name2] == NULL){$message 'Error';break;}
        if(
    $_POST[name3] == NULL){$message 'Error';break;}
        if( 
    $_POST[name4] == NULL){$message 'Error';break;}
        break;
        }
    }
    $end[0] = microtime_float();
    echo(
    "<br />$i");//make sure we looped enough;
    //model 1
    $start[1] = microtime_float();
    for(
    $i=0;$i<$ii;$i++)
    {
     if(
    $x==NULL)
     {
    if(
    $_POST[name] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name1] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name2] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name3] == NULL){$message 'Error';}
    if(
    $message == NULL && $_POST[name4] == NULL){$message 'Error';}
     }
    }
    $end[1] = microtime_float();
    echo(
    "<br />$i");//make sure we looped enough;

    //model 2
    $start[2] = microtime_float();
    for(
    $i=0;$i<$ii;$i++)
    {
    switch(
    $x) { 
    case 
    NULL
    if(
    $_POST[name] == NULL){$message 'Error';break;} 
    if( 
    $_POST[name1] == NULL){$message 'Error';break;} 
    if(
    $_POST[name2] == NULL){$message 'Error';break;} 
    if(
    $_POST[name3] == NULL){$message 'Error';break;} 
    if( 
    $_POST[name4] == NULL){$message 'Error';break;} 
    default:  
     break; 
    }  
    }
    $end[2] = microtime_float();
    echo(
    "<br />$i");
    //model 3
    $start[3] = microtime_float();
    for(
    $i=0;$i<$ii;$i++)
    {
    if(
    $x==null)
    do { 
    if(
    $_POST[name] == NULL){$message 'Error';break;} 
    if( 
    $_POST[name1] == NULL){$message 'Error';break;} 
    if(
    $_POST[name2] == NULL){$message 'Error';break;} 
    if(
    $_POST[name3] == NULL){$message 'Error';break;} 
    if( 
    $_POST[name4] == NULL){$message 'Error';break;} 
    break; 
    } while(
    0);  
    }
    $end[3] = microtime_float();
    echo(
    "<br />$i");
    echo 
    "<br />{$ii} exicution of functions on:";
    echo 
    '<br />Method 0 (while($x==NULL))';
    echo 
    "<br />TIME start:";
    echo 
    $start[0];
    echo 
    "<br />TIME end:".$end[0];
    echo 
    "<br />Total:". ($end[0]-$start[0]);
    echo 
    "<br />Method 1 if statements";
    echo 
    "<br />TIME start:";
    echo 
    $start[1];
    echo 
    "<br />TIME end:".$end[1];
    echo 
    "<br />Total:". ($end[1]-$start[1]);
    echo 
    "<br />Method 2 switch statement";
    echo 
    "<br />TIME start:";
    echo 
    $start[2];
    echo 
    "<br />TIME end:".$end[2];
    echo 
    "<br />Total:". ($end[2]-$start[2]);
    echo 
    "<br />Method 3 do while statement";
    echo 
    "<br />TIME start:";
    echo 
    $start[3];
    echo 
    "<br />TIME end:".$end[3];
    echo 
    "<br />Total:". ($end[3]-$start[3]);
    ?>
    result:
    Code:
    100000
    100000
    100000
    100000
    100000 exicution of functions on:
    Method 0 (while($x==NULL))
    TIME start:1134151549.1521
    TIME end:1134151551.3307
    Total:2.17853307724
    Method 1 if statements
    TIME start:1134151551.3307
    TIME end:1134151552.2747
    Total:0.94402503967285
    Method 2 switch statement
    TIME start:1134151552.2748
    TIME end:1134151554.4805
    Total:2.2057571411133
    Method 3 do while statement
    TIME start:1134151554.4806
    TIME end:1134151556.6372
    Total:2.156574010849
    Then I redid the code and set the first name field to NULL
    [code]
    100000
    100000
    100000
    100000
    100000 exicution of functions on:
    Method 0 (while(==NULL))
    TIME start:1134152388.5179
    TIME end:1134152389.1728
    Total:0.65485286712646
    Method 1 if statements
    TIME start:1134152389.1728
    TIME end:1134152390.1443
    Total:0.97142481803894
    Method 2 switch statement
    TIME start:1134152390.1443
    TIME end:1134152390.8059
    Total:0.66160798072815
    Method 3 do while statement
    TIME start:1134152390.806
    TIME end:1134152391.4304
    Total:0.62441897392273
    [code]

    As you can see the looping methods are now faster, however i doubt people will be consistantly putting NULL data into the page. And this is only for name, not name1.

    Running the code where name = george and name1 = NULL

    Code:
    100000
    100000
    100000
    100000
    100000 exicution of functions on:
    Method 0 (while(==NULL))
    TIME start:1134152524.285
    TIME end:1134152525.3368
    Total:1.0518748760223
    Method 1 if statements
    TIME start:1134152525.3369
    TIME end:1134152526.2928
    Total:0.95589590072632
    Method 2 switch statement
    TIME start:1134152526.2934
    TIME end:1134152527.3676
    Total:1.0742230415344
    Method 3 do while statement
    TIME start:1134152527.3676
    TIME end:1134152528.3988
    Total:1.0312068462372
    As you can see, the looping statements have now become less efficeint than the if statements.


    The results show that the if statements are faster, followed by the do-while method where the switch method and the while method were the slowest. In cases when there is at least some data entered.

    With 100,000 iterations done to each section of code. We are really talking about a microseconds difference between each.

    it does show that if you really want to be fancy, you should use the do-while version instead of the while version of code.

    The IF statements in most cases are faster than using a looping structure.
    Last edited by Korvan; 12-09-2005 at 02:23 PM.

  13. #13
    Join Date
    Nov 2005
    Posts
    268
    I did find an error in my above code, $message needed to be set back to null before another iteration of the next statement. However when i reran the tests it still returned the same relitive result.

    I also ran a multiple else if statement and came up with the same speed as a loop.

    I ran nnormals code and determined it to provide an invalid result for a proper submission, there is no $_POST[name0]. after i corrected it, it produced this result
    slowest: correct input
    Code:
    //set $_POST[name0] = $_POST[name];
    100000 exicution of functions on:
    Method 4 for loop
    TIME start:1134158738.9953
    TIME end:1134158740.0896
    Total:1.094269990921
    fastest (error on first value):
    Code:
    100000
    100000 exicution of functions on:
    Method 4 for loop
    TIME start:1134158867.5235
    TIME end:1134158867.9149
    Total:0.39135003089905
    So my results show that this for method is more effecient than the if method because it doesnt become that much slower with proper input. (its only slightly slower than the if statement.)

    Then i ran luki's code (after adding in the name -> 'first error' in order to complete the code)

    and it came up with this:
    slowest:
    Code:
    100000
    100000 exicutions of functions on:
    Method 5 foreach loop
    TIME start:1134159173.7636
    TIME end:1134159175.3574
    Total:1.59384799003
    fastest
    Code:
    100000
    100000 exicutions of functions on:
    Method 5 foreach
    TIME start:1134159269.421
    TIME end:1134159270.1552
    Total:0.73419618606567

  14. #14
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    I have register globals on.
    Why do have this on?
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  15. #15
    Join Date
    Aug 2005
    Location
    EIB Network
    Posts
    474
    I think goto statements is the clear winner in this speedy situation. They'll light your hair on fire.

    haha sike... don't listen to me... I think the || (OR) or XOR is what you want.

Posting Permissions

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