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...
For further actions, you may consider blocking this person and/or reporting abuse
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 🙂
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.
So, once it's compiled, wouldn't there be no performance difference between
\count
andcount
?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.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...
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.
about the last point what is the minimum php version required?
Namespacing became part of PHP in 5.3. My guess would be >= 5.3 .
php.net/manual/en/language.namespa...
Anyway, if you're using anything less than 7.1 in 2019 you're doing something wrong :p
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
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 ?
I used 3v4l.org/
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
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
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).
Yes, I'm pretty sure opcache is making those changes pointless ;)
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 :)
Nice article. I never use Opache before, I'll try it. Thank mate.
Right!
Good to know, but if the php code performance is really important you can use phalcon framework