Jump to content


Photo

Speed comparison


  • Please log in to reply
16 replies to this topic

Poll: Which is faster? (17 member(s) have cast votes)

Which is faster?

  1. Array (5 votes [100.00%])

    Percentage of vote: 100.00%

Vote Guests cannot vote

#1 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 27 May 2006 - 07:55 PM

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.

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 27 May 2006 - 08:07 PM

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\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 27 May 2006 - 10:17 PM

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

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

Averages:
Option 1 : 0.431189563539
Option 2 : 0.316342406803

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#4 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 28 May 2006 - 10:27 AM

I ran it 5 times and averaged it out..

Run 1
Option 1 : 0.0522909164429
Option 2 : 0.0502500534058

Run 2
Option 1 : 0.0522179603577
Option 2 : 0.051864862442

Run 3
Option 1 : 0.0519139766693
Option 2 : 0.0503580570221

Run 4
Option 1 : 0.0529820919037
Option 2 : 0.0549299716949

Run 5
Option 1 : 0.0544118881226
Option 2 : 0.0863101482391

Avg
Option 1 Avg: 0.05276336669924
Option 2 Avg: 0.05874261856078

Winner: Option 1

PHP 5 on a 1.7ghz Intel P4 Server.

#5 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 May 2006 - 11:50 AM

I started a session and replaced the bottom couple of lines by:

$_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 />';

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...

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 May 2006 - 11:56 AM

here is my results:
Run1
Option 1 : 0.0095610618591309
Option 2 : 0.1908130645752

Run2
Option 1 : 0.073547124862671
Option 2 : 0.0056939125061035

Run3
Option 1 : 0.0068960189819336
Option 2 : 0.029973983764648

Run4
Option 1 : 0.0096101760864258
Option 2 : 0.065150022506714

Run5
Option 1 : 0.0096230506896973
Option 2 : 0.14893484115601

Average
Option 1 : 0.02184748649597172
Option 2 : 0.0881131649017351

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.

#7 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 May 2006 - 11:59 AM

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:

Option 1 average over 200: 0.003299742937088 
Option 2 average over 200: 0.0041548562049866


#8 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 28 May 2006 - 12:07 PM

Option 1 : 0.07028603553772
Option 2 : 0.028390884399414
Life's too short for arguing.

#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 28 May 2006 - 12:10 PM

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.

#10 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 May 2006 - 12:26 PM

[!--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

Info: PHP Manual


#11 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 30 May 2006 - 03:18 PM

[!--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

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 30 May 2006 - 07:56 PM

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

Average:
    Option 1 :  0.0094212770462
    Option 2 :  0.00904858112335 
Winner : Option 2

I tried a 3rd option too, removing the string parsing

    $t3 = timer();
    for ($i=0; $i<1000; $i++) {
        echo 'Name: ', $Name, '<br />School: ', $School, '<br />';
    }
    $t4 = timer();

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.

Average:
    Option 1 :  0.011035990715
    Option 2 :  0.0129390954971
    Option 3 :  0.00881361961365 
Winner : Option 3

Average:    
    Option 1 :  0.0090204000473     
    Option 2 :  0.0170489549637     
    Option 3 :  0.016158747673 
Winner : Option 1

Average:    
    Option 1 :  0.0132476806641     
    Option 2 :  0.00785770225525     
    Option 3 :  0.00790569782257 
Winner : Option 2

Conclusion - too close to call. Use your preferred coding style.

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 14 June 2006 - 06:11 PM

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.
z..z..z..z..z..z..z..z..

#14 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 14 June 2006 - 07:11 PM

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)
#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 



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)
#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 


#15 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 17 September 2006 - 12:02 AM

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:
echo "Name: $Name <br />\r\n".
    "School: $School <br />\r\n";
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%)


#16 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 17 September 2006 - 02:05 PM

I don't think your results were any different from anyone else's then.  We all got mixed results for the most part.

Info: PHP Manual


#17 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 17 September 2006 - 08:25 PM

I did a full test of outputting data, including nesting php here, and came up with some pretty interesting results.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users