Barand Posted May 27, 2006 Share Posted May 27, 2006 Based on a question posed in this topic[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=94342\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=94342[/a]If you run the code below, which comes out faster on your serverOption 1, which hops in and out of PHP, orOption 2, which parses the string?Just curious as we seem to have mixed results so far.[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/10598-speed-comparison/ Share on other sites More sharing options...
.josh Posted May 27, 2006 Share Posted May 27, 2006 well since i ran the script i thought i'd do the poll. since i already posted the results in the other thread, i won't post them here. this looks like an interesting poll [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39534 Share on other sites More sharing options...
neylitalo Posted May 27, 2006 Share Posted May 27, 2006 I've run it 9 times, using the PHP CLI.Attempt 1: [code]Option 1 : 0.603678941727Option 2 : 0.286610126495[/code]Attempt 2: [code]Option 1 : 0.550763130188Option 2 : 0.43427491188[/code]Attempt 3: [code]Option 1 : 0.318045139313Option 2 : 0.107208967209[/code]Attempt 4: [code]Option 1 : 0.244568109512Option 2 : 0.107848882675[/code]Attempt 5: [code]Option 1 : 0.474720954895Option 2 : 0.508259057999[/code]Attempt 6: [code]Option 1 : 0.475308895111Option 2 : 0.506058931351[/code]Attempt 7: [code]Option 1 : 0.476417064667Option 2 : 0.28511595726[/code]Attempt 8: [code]Option 1 : 0.546325922012Option 2 : 0.504494905472[/code]Attempt 9: [code]Option 1 : 0.190877914429Option 2 : 0.107209920883[/code]Averages: [code]Option 1 : 0.431189563539Option 2 : 0.316342406803[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39547 Share on other sites More sharing options...
Prismatic Posted May 28, 2006 Share Posted May 28, 2006 I ran it 5 times and averaged it out..Run 1[code]Option 1 : 0.0522909164429Option 2 : 0.0502500534058[/code]Run 2[code]Option 1 : 0.0522179603577Option 2 : 0.051864862442[/code]Run 3[code]Option 1 : 0.0519139766693Option 2 : 0.0503580570221[/code]Run 4[code]Option 1 : 0.0529820919037Option 2 : 0.0549299716949[/code]Run 5[code]Option 1 : 0.0544118881226Option 2 : 0.0863101482391[/code]Avg[code]Option 1 Avg: 0.05276336669924Option 2 Avg: 0.05874261856078[/code]Winner: Option 1 PHP 5 on a 1.7ghz Intel P4 Server. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39665 Share on other sites More sharing options...
448191 Posted May 28, 2006 Share Posted May 28, 2006 I started a session and replaced the bottom couple of lines by:[code]$_SESSION['option1'][] = $t2-$t1;$_SESSION['option2'][] = $t3-$t2;echo 'Option 1 average over '.count($_SESSION['option1']).': '.array_sum($_SESSION['option1'])/count($_SESSION['option1']).' <br />';echo 'Option 2 average over '.count($_SESSION['option2']).': '.array_sum($_SESSION['option2'])/count($_SESSION['option2']).' <br />';[/code]Result after 200 runs:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 average over 200: 0.0033021557331085 Option 2 average over 200: 0.01090927362442 [/quote]Significant difference in favor of option 1, surprised me a bit... Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39672 Share on other sites More sharing options...
wildteen88 Posted May 28, 2006 Share Posted May 28, 2006 here is my results:Run1[code]Option 1 : 0.0095610618591309Option 2 : 0.1908130645752[/code]Run2[code]Option 1 : 0.073547124862671Option 2 : 0.0056939125061035[/code]Run3[code]Option 1 : 0.0068960189819336Option 2 : 0.029973983764648[/code]Run4[code]Option 1 : 0.0096101760864258Option 2 : 0.065150022506714[/code]Run5[code]Option 1 : 0.0096230506896973Option 2 : 0.14893484115601[/code]Average[code]Option 1 : 0.02184748649597172Option 2 : 0.0881131649017351[/code]I ran the script 5 times on my home PC that has Apache2 andPHP5 install with an Intel P4 3.06Ghz Processor running XP HomeEveryone will have different results as everone will PHP setup will not be the same and everyone will be running PHP different system setups so the results are going to be completly different. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39674 Share on other sites More sharing options...
448191 Posted May 28, 2006 Share Posted May 28, 2006 The biggest diffrence between the two options is caused by your use of double quotes!I replaced all the double quotes with single quotes in your testscript. Result:[code]Option 1 average over 200: 0.003299742937088 Option 2 average over 200: 0.0041548562049866 [/code] Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39676 Share on other sites More sharing options...
hvle Posted May 28, 2006 Share Posted May 28, 2006 Option 1 : 0.07028603553772 Option 2 : 0.028390884399414 Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39679 Share on other sites More sharing options...
448191 Posted May 28, 2006 Share Posted May 28, 2006 It might be a good idea to set up some rules for this experiment, like:1) Run a minimum of 100 test2) Provide the averages3) Provide procentual diffrence (which is the most interesting, because these absolute values are hard to compare)[quote]Option 1 average over 266: 0.0032995652435417 Option 2 average over 266: 0.0041864034824802 Option 1 takes 78.8162% of the time option 2 needs. [/quote]Percentage with double quotes:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 takes 30.2693% of the time option 2 needs. [/quote]EDIT: funny thing: I was running automated tests using:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<META HTTP-EQUIV="Refresh" CONTENT="1; url=<?php $_SERVER['PHP_SELF'];?>">[/quote]After 1500 runs it more or less leveled out on 84-88%... Then I dicided to speed things up changing the refresh value to 0.5, then the percentage dropped to 49-52% ???? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]EDIT2: Found out it matters in which order you run the tests! If I switch the echoing parts, the first option you test is always faster than the second. Nesting php in html does seem slower considered this. With the nesting as option 1, it took about 11% of the time option 2 took.With the nesting as option 2, option 1 took about 7.5% of the time the nesting took. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39680 Share on other sites More sharing options...
ober Posted May 30, 2006 Share Posted May 30, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.011514186859131 Option 2 : 0.021591901779175 [/quote][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.013145923614502 Option 2 : 0.018763065338135[/quote]And I ran about 10 more attempts and the numbers all came out the same pretty much. Option 1 was the clear winner.I'm running IIS 5 on a Win2000 Server box with a 850MHz AMD and 256MB RAM. I'm using PHP 5.0.4 Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40225 Share on other sites More sharing options...
zq29 Posted May 30, 2006 Share Posted May 30, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00474214553833Option 2 : 0.00638699531555 [/quote][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00236105918884Option 2 : 0.101538896561 [/quote][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00477313995361Option 2 : 0.00589203834534 [/quote][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00440788269043Option 2 : 0.00569200515747 [/quote][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00236201286316Option 2 : 0.0874071121216 [/quote]Run on my office machine: Apache 2.0.47, PHP 4.3.11, Win XP Pro, AMD Athlon XP 2200+, 1024MiB RAM Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40257 Share on other sites More sharing options...
Barand Posted May 30, 2006 Author Share Posted May 30, 2006 My previous tests were on PHPEd's built-in server.Running a series of tests on IIS I got alternating results, most pretty close eg[code]Average: Option 1 : 0.0094212770462 Option 2 : 0.00904858112335 Winner : Option 2[/code]I tried a 3rd option too, removing the string parsing[code] $t3 = timer(); for ($i=0; $i<1000; $i++) { echo 'Name: ', $Name, '<br />School: ', $School, '<br />'; } $t4 = timer();[/code]After repeating the tests I still get alternating winners. I guess it depends on any other background tasks that happen to be running during the loop.[code]Average: Option 1 : 0.011035990715 Option 2 : 0.0129390954971 Option 3 : 0.00881361961365 Winner : Option 3[/code][code]Average: Option 1 : 0.0090204000473 Option 2 : 0.0170489549637 Option 3 : 0.016158747673 Winner : Option 1[/code][code]Average: Option 1 : 0.0132476806641 Option 2 : 0.00785770225525 Option 3 : 0.00790569782257 Winner : Option 2[/code]Conclusion - too close to call. Use your preferred coding style. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40356 Share on other sites More sharing options...
joquius Posted June 14, 2006 Share Posted June 14, 2006 Server average results:Option 1 : 0.78Option 2 : 0.64Hmm I got the highest times, must mean I'm the winner. ehrmm...Shared hosting, what do you expect, no point to denote the amazing computer on my server.php 4.4.1, apache 1.3.34, (running linux)Now for my home pc:Option 1 : 0.0016829967498779Option 2 : 0.014244079589844 Option 1 : 0.0035378932952881Option 2 : 0.027537107467651 Option 1 : 0.003410816192627Option 2 : 0.027882099151611 Option 1 : 0.0046489238739014Option 2 : 0.027939081192017 Option 1 : 0.0034699440002441Option 2 : 0.012740850448608 hmm strange observation: instance 2-4 are refreshes. Every time it's a refresh option 2 comes out 3 times more. The first instance was the fastest (the first load).This is 3.2ghz p4, 1gb 533mhz ddr2, running xp home *groan*, apache 2.0.58, php 5.1.4.So my server runs option 1 slower in comparison, differing from my home pc. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-45652 Share on other sites More sharing options...
Koobi Posted June 14, 2006 Share Posted June 14, 2006 System: Linux ubuntu 2.6.12-10-386 #1 Fri Apr 28 13:13:44 UTC 2006 i686 GNU/LinuxPHP: 4.4.0 (apache module)[code]#1Option 1 : 0.00262498855591Option 2 : 0.0545389652252 #2Option 1 : 0.0826070308685Option 2 : 0.0269320011139 #3Option 1 : 0.131360054016Option 2 : 0.0354020595551 [/code]System: Linux lemonhead 2.4.32-grsec+f6b+gr217+nfs+a32+fuse23+++opt+c6+gr2b-v6.192 #1 SMP Wed Dec 14 17:06:16 PST 2005 i686 GNU/LinuxPHP: 4.4.2 (cgi)[code]#1Option 1 : 1.8764750957489Option 2 : 0.004478931427002#2Option 1 : 0.02643609046936Option 2 : 1.8777549266815 #3Option 1 : 0.0059609413146973Option 2 : 0.004857063293457 [/code] Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-45686 Share on other sites More sharing options...
mainewoods Posted September 17, 2006 Share Posted September 17, 2006 your tests are not equal because the first loop will output at least 2 and maybe 3 extra line feeds per loop ('\r\n') because of being in html mode. As well indenting the items in the first loop means extra spaces or an extra tab wil be sent. The second loop must be changed:[code]echo "Name: $Name <br />\r\n". "School: $School <br />\r\n";[/code]when I made those adjustments I got mixed results. Strangely, the first loop seemed to vary more wildly (+/-40%) than the second which stayed close to the same results every time (+/-10%) Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-93291 Share on other sites More sharing options...
ober Posted September 17, 2006 Share Posted September 17, 2006 I don't think your results were any different from anyone else's then. We all got mixed results for the most part. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-93485 Share on other sites More sharing options...
448191 Posted September 17, 2006 Share Posted September 17, 2006 I did a full test of outputting data, including nesting php [url=http://www.phpfreaks.com/forums/index.php/topic,108402.msg436134.html#msg436134]here[/url], and came up with some pretty interesting results. Quote Link to comment https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-93669 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.