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.
I created an array with more than 4 million elements having more than 3 million duplicates. Here is the top result:
execution time: 787.31 ms
execution time: 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.
$array[mt_rand(0, count($array) - 1)];
I created an array with 5 million elements. Here is the top result:
execution time: 25.99 μs
execution time: 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.
I created an array with more than 100k alphanumeric and non-alphanumeric strings. Here is the top result:
execution time: 15.39 ms
execution time: 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.
str_replace('a', 'b', $string);
strtr($string, 'a', 'b');
I created an array with 5 million random strings. Here is the top result:
execution time: 676.59 ms
execution time: 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 switch 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.
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.
Originally published: 14th April 2020 on DEV.to