Web Hosting Talk







View Full Version : Two situations, which is more efficent than the other?


azizny
12-08-2005, 05:16 PM
I have a data checking for a post, the post consitits of x number of fields..

Is this better:


<?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:


<?
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,

malenski
12-08-2005, 05:24 PM
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.

QNS Web Hosting
12-08-2005, 05:59 PM
I have a data checking for a post, the post consitits of x number of fields..

Is this better:


<?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:


<?
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.

heymrdj
12-08-2005, 06:03 PM
Go the second one. More efficient.

luki
12-09-2005, 03:03 AM
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 :).

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

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

arkin
12-09-2005, 03:17 AM
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.

maxymizer
12-09-2005, 06:49 AM
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:



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!

azizny
12-09-2005, 08:00 AM
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,

sea otter
12-09-2005, 08:37 AM
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!

folsom
12-09-2005, 09:58 AM
OMG. Stop thinking about this. It makes no real world difference.

nnormal
12-09-2005, 11:45 AM
OMG. Stop thinking about this. It makes no real world difference.

hehe. quoted for truth.
helps pass the time though...



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

Korvan
12-09-2005, 02:08 PM
I did some testing on your suggestions and some of my own, and I found that the IF statements themselves are MUCH faster.


<?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:

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

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.

Korvan
12-09-2005, 04:16 PM
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

//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):

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:

100000
100000 exicutions of functions on:
Method 5 foreach loop
TIME start:1134159173.7636
TIME end:1134159175.3574
Total:1.59384799003

fastest

100000
100000 exicutions of functions on:
Method 5 foreach
TIME start:1134159269.421
TIME end:1134159270.1552
Total:0.73419618606567

Burhan
12-09-2005, 05:02 PM
I have register globals on.

Why do have this on?

seodevhead
12-09-2005, 05:38 PM
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.