Jump to content

Archived

This topic is now archived and is closed to further replies.

Mateobus

Speed Question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Why don't you test it to find out?

[code]
<?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 />";
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
My results

Option 1 : 0.0608580112457
Option 2 : 0.0299220085144

So hopping in and out of PHP takes longer

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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\" /]

Share this post


Link to post
Share on other sites
[b]Dev - Windows laptop[/b]
Option 1 : 3.17229294777
Option 2 : 0.764003038406

Option 1 : 1.86993980408
Option 2 : 0.345380067825

Option 1 : 1.91975998878
Option 2 : 0.444586038589

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

Option 1 : 1.7756929397583
Option 2 : 0.92465806007385

Option 1 : 0.51655387878418
Option 2 : 0.49194598197937

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.