PHP micro-optimization

有些话、适合烂在心里 提交于 2020-01-02 09:42:27

问题


How can I spot useless micro-optimization techniques?

What should be avoided?


回答1:


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!




回答2:


Write code that works and is readable. If you find it sluggish, you can always do some profiling.




回答3:


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.



来源:https://stackoverflow.com/questions/4236533/php-micro-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!