Jump to content


Photo

Speed Question


  • Please log in to reply
7 replies to this topic

#1 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 26 May 2006 - 09:05 PM

Hey all, I have a speed question, here are two code snippets, which will run faster?
---------------------------- Code --------------------------------
// This is Option 1
<html>
Name:<?php echo $Name; ?> <br />
School:<?php echo $school; ?> <br />
</html>
----------------------------End Code ----------------------------

---------------------------- Code --------------------------------
// This is Option 2
<html>
<?php
echo “Name: $Name <br />”.
“School: $School <br />”;
?>
</html>
----------------------------End Code ---------------------------
This is a very simplified version of the issue i am dealing with. For example, if I am writing complicated html code that creates tables, should I write that using a php echo statement, or use html and make several php calls for each piece of data...

Hope you understand the question that I am asking. Thanks


#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 26 May 2006 - 09:13 PM

Truthfully, I wouldn't know which one will run faster if they are not the same. But if the HTML output is so big that it will effect the script time by a second, The page will take forever to download to the browser.

NoGray.com


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 May 2006 - 10:56 PM

Why don't you test it to find out?

<?php
function timer()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}


$Name = "Donald Duck";
$School = "Slough Grammar";

$t1 = timer();
for ($i=0; $i<1000; $i++) {
?>
    Name:<?php echo $Name; ?> <br />
    School:<?php echo $School; ?> <br />
<?php
}
$t2 = timer();
for ($i=0; $i<1000; $i++) {
    echo "Name: $Name <br />".
    "School: $School <br />";
}
$t3 = timer();

$total1 = $t2-$t1;
$total2 = $t3-$t2;
echo "Option 1 :  $total1 <br />";
echo "Option 2 :  $total2 <br />"; 
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 May 2006 - 11:03 PM

i think that echoing the html through php would be slower. The more php, the longer it takes to parse, and therefore the longer it takes to send to you.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 May 2006 - 11:06 PM

My results

Option 1 : 0.0608580112457
Option 2 : 0.0299220085144

So hopping in and out of PHP takes longer
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Mateobus

Mateobus
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 27 May 2006 - 04:48 PM

Interesting...Several page refreshes say that option 2 takes longer.

Option 1 : 0.0028409957885742
Option 2 : 0.0035510063171387

Option 1 : 0.0027570724487305
Option 2 : 0.0030298233032227

Option 1 : 0.0025520324707031
Option 2 : 0.0031177997589111

thanks for the timer code, looks like i go the faster server barand!

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 May 2006 - 05:13 PM

results on my server:

Option 1 : 0.23969197273254
Option 2 : 0.097437143325806

Option 1 : 0.26200318336487
Option 2 : 0.1240758895874

Option 1 : 0.23777079582214
Option 2 : 0.095417976379395

option 2 seems to be significantly faster on my server :/

well it's a good thing i tend to write my scripts in the form of option 2 anyways [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 jajtiii

jajtiii
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 28 May 2006 - 11:27 AM

Dev - Windows laptop
Option 1 : 3.17229294777
Option 2 : 0.764003038406

Option 1 : 1.86993980408
Option 2 : 0.345380067825

Option 1 : 1.91975998878
Option 2 : 0.444586038589

QA/Prod - Linux Virtual Instance
Option 1 : 0.87854886054993
Option 2 : 0.3418550491333

Option 1 : 1.7756929397583
Option 2 : 0.92465806007385

Option 1 : 0.51655387878418
Option 2 : 0.49194598197937




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users