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

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

+ 294 %

foreach($aHash as $val);

Total time: 11 µsview code

+ 100 %

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

Total time: 4 µsview code

+ 369 %

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

Total time: 14 µsview code

+ 156 %

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

Total time: 6 µsview code

+ 313 %

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

Total time: 12 µsview code

Conclusion:

In all cases I've found that the foreach loop is substantially faster than the 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

Modify Loop: foreach() vs. for

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.

+ 15799 %

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

Total time: 3616 µsview code

+ 100 %

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

Total time: 23 µsview code

Conclusion:

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

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: 23 µsview code

+ 160 %

Without pre calc - count()

Total time: 37 µsview code

+ 100 %

With pre calc - sizeof()

Total time: 23 µsview code

+ 160 %

Without pre calc - sizeof()

Total time: 37 µ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.

Counting LoopsFor vs. While

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

+ 100 %

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

Total time: 22291 µsview code

+ 100 %

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

Total time: 22240 µsview code

Conclusion:

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

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

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

+ 120 %

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

Total time: 60 µsview code

+ 100 %

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

Total time: 50 µsview code

+ 102 %

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

Total time: 51 µsview code

+ 102 %

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

Total time: 51 µsview code

+ 102 %

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

Total time: 51 µsview code

+ 100 %

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

Total time: 50 µsview code

+ 102 %

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

Total time: 51 µ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!

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

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

+ 110 %

isSet() with var that was set

Total time: 100 µsview code

+ 137 %

empty() with var that was set

Total time: 125 µsview code

+ 102 %

isSet() with var that was *not* set

Total time: 93 µsview code

+ 103 %

empty() with var that was *not* set

Total time: 94 µsview code

+ 101 %

isSet() with array-var that was set

Total time: 92 µsview code

+ 138 %

empty() with array-var that was set

Total time: 126 µsview code

+ 100 %

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

Total time: 91 µsview code

+ 103 %

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

Total time: 94 µsview code

+ 105 %

is_array() of an array

Total time: 96 µsview code

+ 105 %

is_array() of a string

Total time: 96 µ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))

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

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

+ 310 %

if and elseif (using ==)

Total time: 152 µsview code

+ 310 %

if, elseif and else (using ==)

Total time: 152 µsview code

+ 112 %

if and elseif (using ===)

Total time: 55 µsview code

+ 100 %

if, elseif and else (using ===)

Total time: 49 µsview code

+ 328 %

switch / case

Total time: 161 µsview code

+ 344 %

switch / case / default

Total time: 169 µ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

+ 103 %

echo ''

Total time: 35 µsview code

+ 100 %

print ''

Total time: 34 µsview code

+ 355 %

echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 121 µsview code

+ 355 %

print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Total time: 121 µsview code

+ 352 %

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

Total time: 120 µsview code

+ 1308 %

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

Total time: 446 µsview code

+ 355 %

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

Total time: 121 µsview code

+ 1106 %

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

Total time: 377 µsview code

+ 1258 %

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

Total time: 429 µsview code

+ 1129 %

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

Total time: 385 µsview code

+ 1108 %

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

Total time: 378 µsview code

+ 1262 %

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

Total time: 430 µsview code

+ 1106 %

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

Total time: 377 µ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.