Live data from Hacker News

PHP Performance Benchmarks

maettig.com

11–20 of 49 posts

Re: PHP Performance Benchmarks

#18
post #9

I'd love to see a benchmark of: for ($i = 0; $i vs foreach($users as $user)

There are some similar benchmark here: http://www.phpbench.com/ Intuitively the for loop should be a lot fast as it isn't assigning anything each loop. Although in your example it may be slower because the count($users) would be ran on each iteration of the loop.

It only assigns $user in a foreach when the $user is modified, or if you are trying to outsmart PHP-core and use & to pass it as a reference.

Re: PHP Performance Benchmarks

#19
post #13

The bad performance of count() and the lack of difference in performance between single and double quotes came as a surprise!

If I were writing a PHP parser, then I would convert double-quoted strings of the form "foo $bar baz" to 'foo '.$bar.' baz' when the script is first parsed. So executing the string concatenation in a loop would have no performance difference, because the string is only parsed the first time.

That's just a guess, though, having not actually looked at the code myself.

Re: PHP Performance Benchmarks

#20

I'd love to see a benchmark of: for ($i = 0; $i vs foreach($users as $user)

I just coded up a benchmark and posted the ProTip on coderwall. http://coderwall.com/p/il1tog

You're re-calculating the count() on each iteration. Of course it's slower.

Fix that:

  for($i = 0; $count = count($elements); $i 
Post reply on HN