The PHP Benchmark

setting the record straight

PHPBench.com was constructed as a way to open people's eyes to the fact that not every PHP code snippet will run at the same speed. You may be surprised at the results that this page generates, but that is ok. This page was also created so that you would be able to find discovery in these statistics and then maybe re-run these tests in your own server environment to play around with this idea yourself, by using the code examples (these code examples are automatically generated and as the code in my .php files change, so do they).

NOTE: You must keep in mind to refresh this page a few times to "catch" the right result. The numbers change sometimes drastically during each refresh. I assume that this is because of PHP's memory garbage collector that drops in randomly and also other processes that run on this machine have an influence.

NOTE: The microtime() method for testing out these tests has only been utilised for simplicities sake. If anything more advanced was used, those whom are just starting out on their PHP journey would have difficulties understanding.

Read Loop:foreach() vs. for() vs. while(list() = each())

What is the best way to loop a hash array?

Given is a Hash array with 100 elements, 24byte key and 10k data per entry

In all cases I've found that the foreach loop is substantially faster than both the while() and for() loop procedures. One thing to note is that when using an entire loop from the start it's extremely good to use the reset() function in all examples

Given that the previous version of the tests have been very controvercial and incorrect, I must appologise for forgetting to implement the reset() function to allow the while() loops to start from the beginning instead of the end. Thanks to Anthony Bush for spotting this out.

Counting LoopsFor-loop test

Is it worth the effort to calculate the length of the loop in advance?

Unsurprising results... this is one of the easiest things to implement in any application and is the widest agreed upon benchmarking item within the online PHP community. The results basically speak for themselves.

Using the =&-ref-operator$obj = new SomeClass() vs. $obj =& new SomeClass()

Is a good idea to use the =&-ref-operator when creating a new object? Call 1'000x

In reality the echo and print functions serve the exact purpose and therefore in the backend the exact same code applies. The one small thing to notice is that when using a comma to separate items whilst using the echo function, items run slightly faster.

Modify Loop:
foreach() vs. for vs. while(list() = each())

What would happen if we alter the reading loop test to test the results of a loop created to simply alter the data in each of the values in the array?

Given again is a Hash array with 100 elements, 24byte key and 10k data per entry.