Mateobus Posted May 26, 2006 Share Posted May 26, 2006 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><?phpecho “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 Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/ Share on other sites More sharing options...
nogray Posted May 26, 2006 Share Posted May 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39290 Share on other sites More sharing options...
Barand Posted May 26, 2006 Share Posted May 26, 2006 Why don't you test it to find out?[code]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39325 Share on other sites More sharing options...
.josh Posted May 26, 2006 Share Posted May 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39330 Share on other sites More sharing options...
Barand Posted May 26, 2006 Share Posted May 26, 2006 My resultsOption 1 : 0.0608580112457 Option 2 : 0.0299220085144 So hopping in and out of PHP takes longer Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39331 Share on other sites More sharing options...
Mateobus Posted May 27, 2006 Author Share Posted May 27, 2006 Interesting...Several page refreshes say that option 2 takes longer.Option 1 : 0.0028409957885742 Option 2 : 0.0035510063171387Option 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! Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39492 Share on other sites More sharing options...
.josh Posted May 27, 2006 Share Posted May 27, 2006 results on my server:Option 1 : 0.23969197273254Option 2 : 0.097437143325806 Option 1 : 0.26200318336487Option 2 : 0.1240758895874 Option 1 : 0.23777079582214Option 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39507 Share on other sites More sharing options...
jajtiii Posted May 28, 2006 Share Posted May 28, 2006 [b]Dev - Windows laptop[/b]Option 1 : 3.17229294777 Option 2 : 0.764003038406Option 1 : 1.86993980408 Option 2 : 0.345380067825Option 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.92465806007385Option 1 : 0.51655387878418 Option 2 : 0.49194598197937 Quote Link to comment https://forums.phpfreaks.com/topic/10538-speed-question/#findComment-39668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.