Jump to content

Speed Question


Mateobus

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.