DEV Community

Cover image for 4 PHP Tricks to Boost Script Performance
Andreas
Andreas

Posted on • Updated on

4 PHP Tricks to Boost Script Performance

Normally I write code by using the conventional, obvious PHP functions to solve corresponding problems. But for some of these problems I came across alternative solutions that especially increase performance.

In this article I want to present some of these alternatives. This is useful, if you're searching for possibilities to decrease execution time even more in production. Let's see, which PHP methods might be replaced by a more performant approach and if there is any cost or trade-off.

All these methods were tested with PHP 7.4 on a local web server

1. Removing duplicates

You have a large array with duplicates and want to remove them to only have an array with unique values only.

🐌 Conventional

array_unique($array);
Enter fullscreen mode Exit fullscreen mode

⚡ Alternative

array_keys(array_flip($array));
Enter fullscreen mode Exit fullscreen mode

⏲ Performance

I created an array with more than 4 million elements having more than 3 million duplicates. Here is the top result:

method execution time
array_unique 787.31 ms
array_keys array_flip 434.03 ms

The alternative approach is 1.8x (44.87%) faster in this measurement. On average, it was ~1.5x (30%) faster. Trade-off: This is only applicable for simple, one-dimensional arrays since array_flip replaces keys by values.

2. Get random array element

You have a large array and want to pick a random value from it.

🐌 Conventional

array_rand($array);
Enter fullscreen mode Exit fullscreen mode

⚡ Alternative

$array[mt_rand(0, count($array) - 1)];
Enter fullscreen mode Exit fullscreen mode

⏲ Performance

I created an array with 5 million elements. Here is the top result:

method execution time
array_rand 25.99 μs
mt_rand 0.95 μs

The alternative approach is 27.3x (96.33%) faster in this measurement. On average, it was ~8x (87%) faster. This result is particularly surprising, as mt_rand is the implementation of the Mersenne Twister Random Number Generator and since PHP 7.1, the internal randomization algorithm has been changed to use exactly that same algorithm.

3. Test for alphanumeric characters

You have a string and want to test, if it only contains alphanumeric characters.

🐌 Conventional

preg_match('/[a-zA-Z0-9]+/', $string);
Enter fullscreen mode Exit fullscreen mode

⚡ Alternative

ctype_alnum($string);
Enter fullscreen mode Exit fullscreen mode

⏲ Performance

I created an array with more than 100k alphanumeric and non-alphanumeric strings. Here is the top result:

method execution time
preg_match 15.39 ms
ctype_alnum 2.06 ms

The alternative approach is 7.5x (86.59%) faster in this measurement. On average, it was ~4x (76%) faster.

The same can be applied to ctype_alpha() (check for alphabetic characters) and ctype_digit() (check for numeric characters).

4. Replace substrings

You have a string and want to replace a part of it by another substring.

🐌 Conventional

str_replace('a', 'b', $string);
Enter fullscreen mode Exit fullscreen mode

⚡ Alternative

strtr($string, 'a', 'b');
Enter fullscreen mode Exit fullscreen mode

⏲ Performance

I created an array with 5 million random strings. Here is the top result:

method execution time
str_replace 676.59 ms
strtr 305.59 ms

The alternative approach is 2.2x (54.83%) faster in this measurement. On average, it was ~2x (51%) faster.

Additional performance improvements

Here are some additional points I integrated into my coding convention that I found to improve perfomance slightly (if applicable):

  • Prefer JSON over XML
  • Declare variables before, not in every iteration of the loop
  • Avoid function calls in the loop header (in for ($i=0; $i<count($array); $i) the count() gets called in every iteration)
  • Unset memory consuming variables
  • Prefer select statement over multiple if statements
  • Prefer require/include over require_once/include_once (ensure proper opcode caching)

Some final words: I know the discussion about premature optimization. And I agree that performance in production is depending on bottlenecks like database queries which should be focused on when dealing with performance. But I think, if there are alternatives that are faster and e.g. in case of regex easier to handle and maintain, why not using them?

Wrap it up

We've seen, that even with the current PHP 7.4 (which is already a lot faster than previous PHP versions) there are possibilities to boost script performance with alternative approaches even more. If you want to verify the figures presented in this article yourself, I created a repository with all tests:

GitHub logo devmount / faster-php

Testing different approaches to improve PHP script performance

I used this great tool by Bart van Hoekelen to measure execution time.

Please don't hesitate to comment here or create an issue/PR at the repo above if you know additional ways to improve performance of certain PHP functions.


Published: 14th April 2020

Top comments (25)

Collapse
 
diek profile image
diek

I readed somewhere that double quotes are more than double fast, it really surprised me.

Collapse
 
devmount profile image
Andreas • Edited

I can confirm that the single vs. double quotes myth is really just a myth. At least for the PHP 7.2+ (maybe this originates from very early PHP versions). I did a quick benchmark:

You can say with caution, that in average, double quotes are slightly (and I really mean very slightly!) faster than single quotes. We're talking about a max of 2ms on 5 million iterations... Obviously only, if no variable is replaced.

Collapse
 
devmount profile image
Andreas • Edited

Double quotes appear to be slower than single quotes because PHP parses their content for variables, e.g.: "a house with $n windows", but on the other hand, single quotes are parsed as well, e.g. 'a \'quoted\' string'

Collapse
 
diek profile image
diek

I'm looking for the source, that's why it surprised me, it has no sense.

Collapse
 
diek profile image
diek • Edited

There it is, simple quote are faster only when it creates an empty string xD
phpbench.com/

Collapse
 
devmount profile image
Andreas

It looks like there is really no significant difference. See this tweet.

Collapse
 
devmount profile image
Andreas

Thank you for this link, this was tested with PHP 7.2, I will retest with the current version of PHP and will present the result 👍🏻

Collapse
 
stojakovic99 profile image
Nikola Stojaković

Well, it's not that surprising, considering double quotes have to process the content.

Collapse
 
diek profile image
diek

Re read, you will see the surprise!

Collapse
 
backendtea profile image
Gert de Pagter

For what its worth, ctype_ functions are locale aware, and may produce slightly different results based on the locale. While preg_match is not locale aware.

Collapse
 
devmount profile image
Andreas

Thank you for this addition! I couldn't find something in the docs about the locales, do you have an example?

Collapse
 
backendtea profile image
Gert de Pagter

It is mentioned in the introduction on ctype php.net/manual/en/intro.ctype.php

Thread Thread
 
devmount profile image
Andreas

Ah thanks 😊

Collapse
 
vinyvicente profile image
Vinicius • Edited

And about the simple or double quotes? What your opinion about the performance of both?

Collapse
 
devmount profile image
Andreas

Do you mean quotes? If you're interested, I could add a ' vs " test...

Collapse
 
vinyvicente profile image
Vinicius

Yes, quotes, my bad. Did you make some implementation of it?

Thread Thread
 
devmount profile image
Andreas

Not yet, but I will create a test with the current PHP version and will come back when I have results 👍🏻

Thread Thread
 
devmount profile image
Andreas

In the meantime, see this:

Thread Thread
 
devmount profile image
Andreas

I did a quick benchmark, see this comment:

I can confirm that the single vs. double quotes myth is really just a myth. At least for the PHP 7.2+ (maybe this originates from ver early PHP versions). I did a quick benchmark:

You can say with caution, that in average, double quotes are slightly (and I really mean very slightly!) faster than single quotes. We're talking about a max of 2ms on 5 million iterations... Obviously only, if no variable is replaced.

Thread Thread
 
vinyvicente profile image
Vinicius

Great! Just for the convention is used so. Thanks, Andreas.

Thread Thread
 
devmount profile image
Andreas

My pleasure 😊

Collapse
 
wapcrazut profile image
Williams Abraham

Nice tricks! I personally never tried the 2nd approach for getting random items from an array, although it's quite obvious haha. Thanks.

Collapse
 
devmount profile image
Andreas

😂 well, everything's fine than I guess! How'd you do it?

Collapse
 
hesamrad profile image
Hesam Rad

Helpful article, thank you very much =)

Collapse
 
devmount profile image
Andreas

You're welcome, I'm glad it's useful for you 😊