Jump to content

Which is faster?  

17 members have voted

  1. 1. Which is faster?

    • Array
      5


Recommended Posts

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 server

Option 1, which hops in and out of PHP, or
Option 2, which parses the string?

Just curious as we seem to have mixed results so far.

[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
https://forums.phpfreaks.com/topic/10598-speed-comparison/
Share on other sites

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\" /]
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39534
Share on other sites

I've run it 9 times, using the PHP CLI.

Attempt 1: [code]Option 1 :  0.603678941727
Option 2 :  0.286610126495[/code]
Attempt 2: [code]Option 1 :  0.550763130188
Option 2 :  0.43427491188[/code]
Attempt 3: [code]Option 1 :  0.318045139313
Option 2 :  0.107208967209[/code]
Attempt 4: [code]Option 1 :  0.244568109512
Option 2 :  0.107848882675[/code]
Attempt 5: [code]Option 1 :  0.474720954895
Option 2 :  0.508259057999[/code]
Attempt 6: [code]Option 1 :  0.475308895111
Option 2 :  0.506058931351[/code]
Attempt 7: [code]Option 1 :  0.476417064667
Option 2 :  0.28511595726[/code]
Attempt 8: [code]Option 1 :  0.546325922012
Option 2 :  0.504494905472[/code]
Attempt 9: [code]Option 1 :  0.190877914429
Option 2 :  0.107209920883[/code]

Averages: [code]Option 1 : 0.431189563539
Option 2 : 0.316342406803[/code]
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39547
Share on other sites

I ran it 5 times and averaged it out..

Run 1
[code]
Option 1 : 0.0522909164429
Option 2 : 0.0502500534058
[/code]

Run 2
[code]
Option 1 : 0.0522179603577
Option 2 : 0.051864862442
[/code]

Run 3
[code]
Option 1 : 0.0519139766693
Option 2 : 0.0503580570221
[/code]

Run 4
[code]
Option 1 : 0.0529820919037
Option 2 : 0.0549299716949
[/code]

Run 5
[code]
Option 1 : 0.0544118881226
Option 2 : 0.0863101482391
[/code]

Avg
[code]
Option 1 Avg: 0.05276336669924
Option 2 Avg: 0.05874261856078
[/code]

Winner: Option 1

PHP 5 on a 1.7ghz Intel P4 Server.
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39665
Share on other sites

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...
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39672
Share on other sites

here is my results:
Run1
[code]Option 1 : 0.0095610618591309
Option 2 : 0.1908130645752[/code]

Run2
[code]Option 1 : 0.073547124862671
Option 2 : 0.0056939125061035[/code]

Run3
[code]Option 1 : 0.0068960189819336
Option 2 : 0.029973983764648[/code]

Run4
[code]Option 1 : 0.0096101760864258
Option 2 : 0.065150022506714[/code]

Run5
[code]Option 1 : 0.0096230506896973
Option 2 : 0.14893484115601[/code]

Average
[code]Option 1 : 0.02184748649597172
Option 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 Home

Everyone 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.
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39674
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39676
Share on other sites

It might be a good idea to set up some rules for this experiment, like:

1) Run a minimum of 100 test
2) Provide the averages
3) 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.
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-39680
Share on other sites

[!--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
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40225
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00474214553833
Option 2 : 0.00638699531555 [/quote]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00236105918884
Option 2 : 0.101538896561 [/quote]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00477313995361
Option 2 : 0.00589203834534 [/quote]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00440788269043
Option 2 : 0.00569200515747 [/quote]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Option 1 : 0.00236201286316
Option 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
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40257
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-40356
Share on other sites

  • 2 weeks later...
Server average results:

Option 1 : 0.78
Option 2 : 0.64

Hmm 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.0016829967498779
Option 2 : 0.014244079589844

Option 1 : 0.0035378932952881
Option 2 : 0.027537107467651

Option 1 : 0.003410816192627
Option 2 : 0.027882099151611

Option 1 : 0.0046489238739014
Option 2 : 0.027939081192017

Option 1 : 0.0034699440002441
Option 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.
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-45652
Share on other sites

System: Linux ubuntu 2.6.12-10-386 #1 Fri Apr 28 13:13:44 UTC 2006 i686 GNU/Linux
PHP: 4.4.0 (apache module)
[code]
#1
Option 1 : 0.00262498855591
Option 2 : 0.0545389652252

#2
Option 1 : 0.0826070308685
Option 2 : 0.0269320011139

#3
Option 1 : 0.131360054016
Option 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/Linux
PHP: 4.4.2 (cgi)
[code]
#1
Option 1 : 1.8764750957489
Option 2 : 0.004478931427002

#2
Option 1 : 0.02643609046936
Option 2 : 1.8777549266815

#3
Option 1 : 0.0059609413146973
Option 2 : 0.004857063293457
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-45686
Share on other sites

  • 3 months later...
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%)
Link to comment
https://forums.phpfreaks.com/topic/10598-speed-comparison/#findComment-93291
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.