PHP micro-optimization
How can开发者_运维知识库 I spot useless micro-optimization techniques?
What should be avoided?
Any optimization done without being measured and profiled first is useless.
PHP code profilers:
- xDebug
- PHP_Debug
- time (Sometimes it is easy to spot bottlenecks in the code using a simple
echo time()
)
Always measure before optimizing!
Write code that works and is readable. If you find it sluggish, you can always do some profiling.
I'm making myself unpopular and say isset
.
To check for undefined variables isset()
is often used throughout application logic. Many people however only use it with the intent to suppress notices. It's use seldomly contributes to further procession logic. And more specifically it is used over @
, the error suppression operator. And that's because there is the @slowness myth.
The thing is, it's not a myth. Using @
for accessing undefined variables drains processing speed. In my very unscientific test, it did so by 535%. I'm making it bold to underline the uselessness of that number. Because in real world applications you won't have 10 million occourences to measure it. (Like the 13-14% tokenizer speedup of 'single' quotes has no impact on the overall script runtime.) Otherwise this performance disadvantage wouldn't really show. And that's why I conclude that eschewing @
for overflowing usage of isset
is also a micro-optimization.
精彩评论