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

+ 130 %

foreach($aHash as $val);

Total time: 26 µsview code

+ 729 %

while(list(,$val) = each($aHash));

Total time: 146 µsview code

+ 100 %

foreach($aHash as $key => $val);

Total time: 20 µsview code

+ 785 %

while(list($key,$val) = each($aHash));

Total time: 157 µsview code

+ 285 %

foreach($aHash as $key=>$val) $tmp[] = $aHash[$key];

Total time: 57 µsview code

+ 849 %

while(list($key) = each($aHash)) $tmp[] = $aHash[$key];

Total time: 170 µsview code

+ 444 %

Get key-/ value-array: foreach($aHash as $key[]=>$val[]);

Total time: 89 µsview code

+ 289 %

Get key-/ value-array: array_keys() / array_values()

Total time: 58 µsview code

+ 405 %

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $tmp[] = $aHash[$key[$i]];

Total time: 81 µsview code

Conclusion:

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?

e.g. "for ($i=0; $i<$size; $i++)" instead of "for ($i=0; $i<sizeOf($x); $i++)"

A loop with 1000 keys with 1 byte values are given.

+ 100 %

With pre calc - count()

Total time: 213 µsview code

+ 81519 %

Without pre calc - count()

Total time: 173561 µsview code

+ 101 %

With pre calc - sizeof()

Total time: 214 µsview code

+ 50618 %

Without pre calc - sizeof()

Total time: 107769 µsview code

Conclusion:

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

+ 105 %

$obj = new SomeClass();

Total time: 600 µsview code

+ 100 %

$obj =& new SomeClass();

Total time: 574 µsview code

Conclusion:

There seams to be no difference in performance.

Using the =&-ref-operator$obj = $someClass->f() vs. $obj =& $someClass->f()

Is a good idea to use the =&-ref-operator when calling a function in an object? Call 1'000x

+ 100 %

$obj = $someClass->f();

Total time: 601 µsview code

+ 243 %

$obj =& $someClass->f();

Total time: 1460 µsview code

Conclusion:

Unless your extremely worried about how much RAM your using, leaving the &-ref-operator out seems like the slightly faster option.

Quote Typesdouble (") vs. single (') quotes

Is a there a difference in using double (") and single (') quotes for strings. Call 1'000x

+ 152 %

single (') quotes. Just an empty string: $tmp[] = '';

Total time: 451 µsview code

+ 102 %

double (") quotes. Just an empty string: $tmp[] = "";

Total time: 302 µsview code

+ 102 %

single (') quotes. 20 bytes Text : $tmp[] = 'aaaaaaaaaaaaaaaaaaaa';

Total time: 303 µsview code

+ 100 %

double (") quotes. 20 bytes Text : $tmp[] = "aaaaaaaaaaaaaaaaaaaa";

Total time: 297 µsview code

+ 101 %

single (') quotes. 20 bytes Text and 3x a $ : $tmp[] = 'aa $ aaaa $ aaaa $ a';

Total time: 300 µsview code

+ 108 %

double (") quotes. 20 bytes Text and 3x a $ : $tmp[] = "aa $ aaaa $ aaaa $ a";

Total time: 321 µsview code

+ 102 %

double (") quotes. 20 bytes Text and 3x a \$ : $tmp[] = "aa \$ aaaa \$ aaaa \$ a";

Total time: 302 µsview code

Conclusion:

In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!

Control Structuresswitch/case/default vs. if/elseif/else

Is a there a difference between switch and if structures?. Call 1'000x

+ 140 %

if and elseif (using ==)

Total time: 194 µsview code

+ 138 %

if, elseif and else (using ==)

Total time: 191 µsview code

+ 102 %

if and elseif (using ===)

Total time: 141 µsview code

+ 100 %

if, elseif and else (using ===)

Total time: 138 µsview code

+ 140 %

switch / case

Total time: 194 µsview code

+ 182 %

switch / case / default

Total time: 251 µsview code

Conclusion:

Using a switch/case or if/elseif is almost the same. Note that the test is unsing === (is exactly equal to) and is slightly faster then using == (is equal to).

String Outputecho vs. print

Is a there a difference between what option you use to output your content?. Called within Output Buffering 1'000x

+ 100 %

echo ''

Total time: 120 µsview code

+ 101 %

print ''

Total time: 121 µsview code

+ 151 %

echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 181 µsview code

+ 161 %

print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 193 µsview code

+ 460 %

echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'

Total time: 553 µsview code

+ 378 %

echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'

Total time: 454 µsview code

+ 459 %

print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'

Total time: 552 µsview code

+ 484 %

$a = 'aaaaaaa';
echo 'aaaaaaa'.$a.'aaaaaaa'.$a

Total time: 582 µsview code

+ 559 %

$a = 'aaaaaaa';
echo 'aaaaaaa',$a,'aaaaaaa',$a

Total time: 671 µsview code

+ 514 %

$a = 'aaaaaaa';
print 'aaaaaaa'.$a.'aaaaaaa'.$a

Total time: 618 µsview code

+ 480 %

$a = 'aaaaaaa';
echo $a.$a.$a.$a

Total time: 577 µsview code

+ 436 %

$a = 'aaaaaaa';
echo $a,$a,$a,$a

Total time: 524 µsview code

+ 502 %

$a = 'aaaaaaa';
print $a,$a,$a,$a

Total time: 603 µsview code

Conclusion:

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.

+ 1047 %

foreach($aHash as $key=>$val) $aHash[$key] .= "a";

Total time: 796 µsview code

+ 214 %

while(list($key) = each($aHash)) $aHash[$key] .= "a";

Total time: 163 µsview code

+ 100 %

$key = array_keys($aHash);
$size = sizeOf($key);
for ($i=0; $i<$size; $i++) $aHash[$key[$i]] .= "a";

Total time: 76 µsview code

Conclusion:

Proof in this example shows how functionally murderous the foreach() loop can be.

Counting LoopsFor vs. While

Is there an actual difference between counting up between the for loop and the while loop?

+ 112 %

for($i = 0; $i < 1000000; ++$i);

Total time: 75746 µsview code

+ 100 %

$i = 0; while($i < 1000000) ++$i;

Total time: 67628 µsview code

Conclusion:

Well there you have it, the while loop 90% of the time is indeed slightly faster

Using the &-ref-operator...as a so called "alias"

Is a good idea to use the &-ref-operator to substitute (or alias) a complex mutidim-array? . Call 1'000x

E.g. $person = &$aHach["country"]["zip"]["street"]["number"]["name"]

+ 228 %

$alias = $aSingleDimArray[$i]

Total time: 879 µsview code

+ 100 %

$alias = &$aSingleDimArray[$i]

Total time: 385 µsview code

+ 225 %

$alias = $aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]

Total time: 866 µsview code

+ 605 %

$alias = &$aMultiDimArray[$i]["aaaaa"]["aaaaaaaaaa"]

Total time: 2329 µsview code

+ 236 %

$alias = veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]

Total time: 908 µsview code

+ 1240 %

$alias = &$veryMultiDimArray[$i]["a"]["aa"]["aaa"]["aaaa"]["aaaaa"]

Total time: 4773 µsview code

Conclusion:

Whilst only using a one dimensional array, it's actually faster to use an alias, but anything larger will result in a performance drop.

Variable Type CheckingisSet() vs. empty() vs. is_array()

What is the performance of isSet() and empty(). Call 2'000x

+ 109 %

isSet() with var that was set

Total time: 209 µsview code

+ 108 %

empty() with var that was set

Total time: 206 µsview code

+ 101 %

isSet() with var that was *not* set

Total time: 192 µsview code

+ 100 %

empty() with var that was *not* set

Total time: 191 µsview code

+ 104 %

isSet() with array-var that was set

Total time: 198 µsview code

+ 117 %

empty() with array-var that was set

Total time: 224 µsview code

+ 103 %

isSet() with array-var that was *not* set

Total time: 197 µsview code

+ 100 %

empty() with array-var that was *not* set

Total time: 191 µsview code

+ 504 %

is_array() of an array

Total time: 963 µsview code

+ 501 %

is_array() of a string

Total time: 956 µsview code

+ 1193 %

is_array() of a non set value

Total time: 2278 µsview code

+ 1173 %

isSet() AND is_array() of a non set value

Total time: 2241 µsview code

Conclusion:

isSet() and empty() are identical. So alway check if val is set at all befor using type-checking. E.g. if (isSet($foo) AND is_array($foo))