DEV Community

Optimizing your PHP app speed

eLabFTW on March 14, 2019

This post is intended for PHP devs. I'll show you four ways to improve the speed of your PHP app easily. I'll skip the part where I tell you to u...
Collapse
 
ddziaduch profile image
Damian Dziaduch

You should really start with checking you SQL queries. You can have ultra fast code, but usually the problem are the queries... And other external calls like APIs. You should really focus on that first 🙂

Collapse
 
elabftw profile image
eLabFTW

Yes sure, but this is not what this post is about ;)

But maybe I can add a sentence saying that all of this won't make your code magically fast and one needs to use profilers and optimize SQLs to get serious.

Collapse
 
goodevilgenius profile image
Dan Jones

The "compiler" (opcode producer) will look into the current namespace, then go up, and eventually use the global namespace.

So, once it's compiled, wouldn't there be no performance difference between \count and count?

So, if I use your first suggestion, I don't need your last suggestion.

And if I use opcache_compile_file on all my PHP files during deployment, an end-user never hits an uncompiled file.

Collapse
 
jdreesen profile image
Jacob Dreesen

Actually the explanation for point "3. Use a backslash in front of standard functions" is not entirely correct. Yes, there will be less opcode produced because PHP only needs to look at the global namespace, but this should barely be noticeable (and would be optimized by the OpCode-Cache on the second run anyways).

But the speed improvement shown in your benchmark does not result from this "less namespace lookup" thing, but from a compiler-optimized "count" function that will be used when calling it this way.

This only works for a handful of functions, though.
You can find the list here: github.com/FriendsOfPHP/PHP-CS-Fix...

Collapse
 
david_j_eddy profile image
David J Eddy

Nice article eLabFTW. The leading backslash for SPL functions also helps to avoid naming conflicts; the reason I first started using it myself. Nice to know there if a measurable positive perf. impact as well.

Collapse
 
mte90 profile image
Daniele Scasciafratte

about the last point what is the minimum php version required?

Collapse
 
david_j_eddy profile image
David J Eddy

Namespacing became part of PHP in 5.3. My guess would be >= 5.3 .

php.net/manual/en/language.namespa...

Collapse
 
elabftw profile image
eLabFTW

Anyway, if you're using anything less than 7.1 in 2019 you're doing something wrong :p

 
juanitomint profile image
Juan Ignacio Borda

Legacy projects can be a pain in the ass that's why IBM is contributing to the 5.x branch of php to give developers and customers time to adapt and migrate, but EOL means it will no longer get security updates or fixes

Collapse
 
sicklounet profile image
Brenier Arnaud

Thanks for the article ! I have been practicing PHP for a long Time but I never digged into performance details.

I have a question though, what did you use for the Benchmark you made with the blackslash ?

Collapse
 
elabftw profile image
eLabFTW

I used 3v4l.org/

Collapse
 
darkain profile image
Vincent Milum Jr

About that last point, it truly is a "micro-optimization", in that, odds are there are other areas that are in the orders of hundreds to thousands of times more CPU intensive within the code that could be optimized using better overall application logic. Micro-optimizations are things we put in that we think will make a difference that really don't in the grand scheme of things.

Measuring the impact of having the leading slash or not results in ~1-8ms better performance across one-million executions. With such a minor difference in performance, a developer's time would honestly be better spent on more significant portions of the application.

3v4l.org/QaY2t

Collapse
 
darkain profile image
Vincent Milum Jr

Doing further investigating, it is actually MORE impactful to not use namespaces at all. If you are not actively within a namespace, and you don't use the leading \, then you get even more "micro-optimization" out of your code! ;)

3v4l.org/tmJMq

Collapse
 
stilldreaming1 profile image
still-dreaming-1

I timed running the entire test suite for the website I work on (not speed tests, functionality tests).

Then I ran some PHP-CS-Fixer rules to add backlashes before native functions and constants throughout our entire codebase. Then I timed the test suite again, and it was basically the same speed (technically it ran slightly slower with the backslashes).

Collapse
 
elabftw profile image
eLabFTW

Yes, I'm pretty sure opcache is making those changes pointless ;)

Collapse
 
elabftw profile image
eLabFTW

Yeah, I made a lot of angry redditors :p But that's not a surprise. Most of the posts on r/php or r/programming are downvoted to oblivion, at least this one got a few tens of upvotes :)

Collapse
 
ardhityawiedhairawan profile image
Ardhitya Wiedha Irawan

Nice article. I never use Opache before, I'll try it. Thank mate.

 
juanitomint profile image
Juan Ignacio Borda

Right!

Collapse
 
nomadzy profile image
Achraf

Good to know, but if the php code performance is really important you can use phalcon framework