optimize PHP snippet

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I know that in this snippet we're talking nanoseconds, but the theory can apply to larger scales....

which is better?

## Example 1 ##
<select name=hour>
<?for ($i=1; $i < date("G",time()); $i++) {?>
  <option value="<?echo $i;?>"><?echo $i;?></option>
<?}?>
  <option value="<?echo $i;?>" SELECTED><?echo $i;?></option>
<?for ($i=date("G",time())+1; $i < 24; $i++) {?>
  <option value="<?echo $i;?>"><?echo $i;?></option>
<?}?> 
</select>


## Example 2 ##
<select name=hour>
<?for ($i=1; $i < 24; $i++) {?>
  <option value="<?echo $i;?>" <?if (date("G",time()) == $i){echo "SELECTED";}?>><?echo $i;?></option>
<?}?>
</select>


## Example 3 ##
<select name=hour>
  <option value="1" <?if (date("G",time()) == 1){echo "SELECTED";}?>>1</option>
  <option value="2" <?if (date("G",time()) == 2){echo "SELECTED";}?>>2</option>
[* edited for length *]
  <option value="22" <?if (date("G",time()) == 22){echo "SELECTED";}?>>22</option>
  <option value="23" <?if (date("G",time()) == 23){echo "SELECTED";}?>>23</option>
</select>
'

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

The code is to small for there to be any measurable difference between the 3. Maybe if there were 2300 items in the select field instead of 23 you would notice a difference.

I would probably pick number 3 to be the fastest since there is only 1 call per item to server-side processing, although this is not the easiest from a coding standpoint since someone has to hardcode all those 2300 items Sad

The quickest and easiest way would be to keep the whole select box server-side until it's populated then send it to the client.

PJ | Are we there yet?
pjboettcher.com

They have: 103 posts

Joined: Apr 1999

I HATE getting the database value of a select field. It always involves too much work. I wish we could just do and that would bring up the correct option. Would save me lots of time...

Gil Hildebrand, Jr.
Internet Consultant
New Orleans, LA

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

wouldn't you think that example 1 would be the best?
if only has to do 2 FOR loops, but example has to do 23 IF statements?

I'm curious... which is more of a load? a FOR loop that repeats 23 times? or 23 IF statements?

maybe I'll time it... but with a thousand or so...
...maybe I'll write it in PHP and Perl and compare the two... hmmm...

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

I don't have a lot of experience with PHP but I do with ASP, and I know for a fact that everytime you invoke server-side processing (

<?php

?>
for asp & <? ?> for PHP) the server takes a performance hit. While it's hard it's always better to try and write code inside one server-side block of code rather than multiple blocks.

Even though the other examples look efficient (and they are) they still make at least 2 call's to server-side processing per line compared to 1 call in example 3.

As for which has more load, a FOR LOOP or IF statement, if they were both inside one block of code then I would probably say the IF statement requires more CPU since it requires logic, the FOR LOOP is basically a counter. A loop is definately more efficient when it's setup to exit when it reaches or finds a certain value. The whole IF statement has to be processed even if it found the right value, that's why I try to stick with SELECT CASE when possible.

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Would this revision be better?

## Example 1 (revision) ##
<select name=hour>
<?
for ($i=1; $i < date("G",time()); $i++) {
  echo "<option value='$i'>$i</option>";
}
  echo "<option value='$i' SELECTED>$i</option>";
for ($i=date("G",time())+1; $i < 24; $i++) {
  echo "<option value='$i'>$i</option>";
}
?> 
</select>
'

Mark Hensler
If there is no answer on Google, then there is no question.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

It's all inside one code block, perfect!

They have: 46 posts

Joined: Dec 2000

While we are on the subject...

I am working on my first asp site now. It's a massive task to take first up, but it is going quite well.

To the point, how much of a performence hit do you think a database call makes (SQL) ? In this site;

http://ww.domaindlx.com/shinnathan/index.asp

... I must make several hundred calls to the database. As the database gets bigger, that number will easily go into the thousands. The site does not seem to load slowly...

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

ShinNathan,

Anytime you make reference to an external object (ADO, COM, etc) the page is going to be slower then if their was no reference to it. Having said that, making connections to a SQL db using ADO is very efficient. Here's some tips to keep your pages fast:

1. Only make one connection per page, if you need to make more than one call to the database on the page re-use your existing connection.

2. Close your connections/recordsets as soon as possible and set them to nothing.

3. Don't pull more data than you need (don't use SELECT *)

4. Use stored procedures if possible, they're faster than SQL inside the ASP.

Have fun with ASP & SQL!

PJ | Are we there yet?
pjboettcher.com

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.