Fork me on GitHub

The table below profiles built-in PHP method performance, loosely grouped by type and usage.

Each test comprises of 1,000 method calls averaged over 100 repetitions.

The median profile takes 0.002367 seconds to run 1,000 times. This value is used to calculate the relative percentage costs across both the group and suite of profiled functions. The value for a single method call is derived by dividing the mean value by the number of iterations, so it is approximate.

You can click on any group's heading to view the profile class as a whole, or you can click on each individual method to be taken to the source code behind each profile – particularly useful if the profile label is a summary rather than actual code.

All of these tests are compiled using the same machine each time. Inevitably, each run may vary, but the differences should be slight and the relative performance should be fairly consistent.

The μs symbol stands for microseconds, equivalent to 0.001 milliseconds. It takes roughly 350,000 of these to blink your eye.

This run took 26.42 seconds and was generated on 18/06/2013 19:02:12

Results

Array methods × 1,000 × 1 Group Suite
array_fill(010'foo'); 0.002622 s 2 μs -57.64% +10.77%
array_fill(-50100'foobar'); 0.006190 s 6 μs 0% +161.51%
array_fill(0100'bar'); 0.006307 s 6 μs +1.89% +166.46%
array_fill(01000'baz'); 0.041673 s 41 μs +573.23% +1660.58%
array_merge(array("foo" => "bar"), array("baz" => "test")); 0.002793 s 2 μs -54.88% +18%
array_merge($this->mediumArray1$this->mediumArray2); 0.021649 s 21 μs +249.74% +814.62%
shuffle($this->mediumArray1); 0.005331 s 5 μs -13.88% +125.22%
Date methods × 1,000 × 1 Group Suite
date("Y-m-d H:i:s"); 0.006645 s 6 μs +1.11% +180.74%
date("Y"); 0.006152 s 6 μs -6.39% +159.91%
date("l jS F Y"); 0.006572 s 6 μs 0% +177.65%
Hashing methods × 1,000 × 1 Group Suite
md5("abcdefghijklmnopqrstuvwxyz"); * 0.002144 s 2 μs -26.55% -9.42%
md5() with ~520 char string * 0.003395 s 3 μs +16.31% +43.43%
sha1("abcdefghijklmnopqrstuvwxyz"); * 0.002443 s 2 μs -16.31% +3.21%
sha1() with ~520 char string * 0.004358 s 4 μs +49.3% +84.11%
PCRE methods × 1,000 × 1 Group Suite
preg_match("/^(.+)$/""test"); 0.002942 s 2 μs -20.68% +24.29%
preg_match("/^(.+)$/""test"$matches); 0.003800 s 3 μs +2.45% +60.54%
preg_match() with typical postcode 0.004069 s 4 μs +9.71% +71.91%
preg_replace('/(\w+) (\d+), (\d+)/i''${1}1,$3''April 15, 2003'); 0.003618 s 3 μs -2.45% +52.85%
Serialisation methods × 1,000 × 1 Group Suite
json_encode("simple string"); 0.002156 s 2 μs -3.41% -8.91%
serialize("simple string"); * 0.002309 s 2 μs +3.45% -2.45%
String functions × 1,000 × 1 Group Suite
echo "foo bar baz test"; 0.001234 s 1 μs -41.85% -47.87%
echo with large string (~520 chars) 0.002306 s 2 μs +8.67% -2.58%
explode(" ""foo bar baz test baz bar foo"); 0.002883 s 2 μs +35.86% +21.8%
explode() on space with large input string 0.013949 s 13 μs +557.35% +489.31%
htmlentities("this 'is' a <strong>test</strong> <div>\"string!?\"</div>"); 0.003897 s 3 μs +83.65% +64.64%
htmlentities() with ENT_QUOTES flag 0.003892 s 3 μs +83.41% +64.43%
htmlspecialchars("this 'is' a <strong>test</strong> <div>\"string!?\"</div>"); 0.002769 s 2 μs +30.49% +16.98%
htmlspecialchars() with ENT_QUOTES flag 0.002805 s 2 μs +32.19% +18.5%
implode(" ", array("foo""bar""baz""test""baz""bar""foo")); 0.003129 s 3 μs +47.46% +32.19%
md5("abcdefghijklmnopqrstuvwxyz"); 0.002144 s 2 μs +1.04% -9.42%
md5() with ~520 char string 0.003395 s 3 μs +59.99% +43.43%
sha1("abcdefghijklmnopqrstuvwxyz"); 0.002443 s 2 μs +15.13% +3.21%
sha1() with ~520 char string 0.004358 s 4 μs +105.37% +84.11%
strlen("some random arbitrary string here"); 0.001902 s 1 μs -10.37% -19.65%
str_pad("TestStr"20STR_PAD_LEFT); 0.002355 s 2 μs +10.98% -0.51%
str_pad("TestStr"20); 0.002114 s 2 μs -0.38% -10.69%
strpos("abcdefghijklmnopqrstuvwxyq""a"); 0.002085 s 2 μs -1.74% -11.91%
strpos("abcdefghijklmnopqrstuvwxyq""m"); 0.002007 s 2 μs -5.42% -15.21%
strpos("abcdefghijklmnopqrstuvwxyq""z"); 0.001979 s 1 μs -6.74% -16.39%
str_replace("foo""bar""foobarfoobar"); 0.002243 s 2 μs +5.7% -5.24%
str_shuffle("abcdefghijklmnopqrstuvwxyz"); 0.002608 s 2 μs +22.9% +10.18%
str_shuffle() with a large (~520 charstring 0.013274 s 13 μs +525.54% +460.79%
addslashes("This string contains nothing which will be replaced"); 0.002073 s 2 μs -2.31% -12.42%
addslashes("'this string' will 'need ' \" \lots of ''' replacement''"); 0.002131 s 2 μs +0.42% -9.97%
crypt("the string to encrypt""salt"); 0.006832 s 6 μs +221.96% +188.64%
lcfirst("This is a test string - uppercase"); 0.001906 s 1 μs -10.18% -19.48%
lcfirst("this is a test string - lowercase"); 0.001959 s 1 μs -7.68% -17.24%
ucfirst("This is a test string - uppercase"); 0.001916 s 1 μs -9.71% -19.05%
ucfirst("this is a test string - lowercase"); 0.001952 s 1 μs -8.01% -17.53%
ucwords("This Is A Test String - Uppercase"); 0.002002 s 2 μs -5.66% -15.42%
ucwords("this is a test string - lowercase"); 0.002013 s 2 μs -5.14% -14.96%
levenshtein("this string matches""this string matches"); 0.003406 s 3 μs +60.51% +43.9%
levenshtein("this is a string""this si string"); 0.002860 s 2 μs +34.78% +20.83%
levenshtein("this is a string""gnirts a si siht"); 0.002999 s 2 μs +41.33% +26.7%
trim("this has no padding"); 0.002036 s 2 μs -4.05% -13.98%
trim("    this string has padding on the left"); 0.002040 s 2 μs -3.86% -13.81%
trim("this string has padding on the right     "); 0.002050 s 2 μs -3.39% -13.39%
trim("     this string has padding on both sides     "); 0.002050 s 2 μs -3.39% -13.39%
"some string"."some other string"; 0.001261 s 1 μs -40.57% -46.73%
"some string$some_other_string" 0.001630 s 1 μs -23.19% -31.14%
string dot concatenation with ~520 char string  0.001499 s 1 μs -29.36% -36.67%
string concatenation with interpolation with ~520 char string  0.001537 s 1 μs -27.57% -35.07%
Variable methods × 1,000 × 1 Group Suite
empty($this->string); 0.001292 s 1 μs -41.78% -45.42%
is_int("string"); 0.001951 s 1 μs -12.08% -17.57%
is_callable("unknownFunction"); 0.002367 s 2 μs +6.67% 0%
serialize("simple string"); 0.002309 s 2 μs +4.06% -2.45%
serialize(12345); 0.002214 s 2 μs -0.23% -6.46%
serialize(true); 0.002219 s 2 μs 0% -6.25%
serialize($this->object); 0.003104 s 3 μs +39.88% +31.14%

The main goal of this project is to illustrate the relative costs of commonly used low-level PHP methods, and to try and give some sort of baseline as to how long these methods can take in isolation. All too often you'll hear methods being dismissed as “expensive” without any form of empirical evidence either way, and similarly you'll hear PHP bemoaned as being “slow” – but of course both these terms are relative. Sure, PHP might be slow compared to other languages, but this can quickly be blown out of proportion and lead people to assume it's actually slow, and that a smattering of functions will bring your application to its knees. These tests simply exist to give some grounding and points-of-reference to these sorts of conversations.

These tests don't run in a vacuum – inevitably no two runs will yield the same results, and even then only serve as a rough baseline based on the machine I run them on – your results may differ significantly. The mechanism used to run and time the tests probably isn't the most scientific either, so if you've got a suggestion then please do fork the project – any improvements are welcome!

phpperf needs you! There are loads of methods and common use-cases not yet profiled. Please see the readme file for how to add your own profiles (it's quick & easy!). Every contributor will be listed here in the order they first helped out with the project. Think of the fame!