Home >Backend Development >PHP Problem >Is php very inefficient?
PHP is not inefficient. On the contrary, PHP has many efficient writing methods. The following are several efficient ways to write PHP:
1. Make it as static as possible
If a method can be static, declare it as static, and the speed can be increased by 1/4. In fact, the main difference in the efficiency of static methods and non-static methods is memory: static methods generate memory when the program starts, and instance methods generate memory while the program is running, so static methods can be called directly, and instance methods must first generate an instance and call it through the instance. Method, static speed is very fast, but too much will occupy memory.
2. Set the maximum number of loops before the loop, instead of inside the loop
3. Destroy variables to release memory, especially large arrays
Arrays and Objects occupy a lot of memory in PHP. This is caused by the underlying zend engine of PHP. Generally speaking, the memory utilization of PHP arrays is only 1/10. That is to say, an array with 100M memory in C language will not be used in PHP. It requires 1G inside.
4. Use absolute paths in includes and requires.
If a relative path is included, PHP will traverse the include_path to find the file. Using absolute paths will avoid such problems, so it will take less time to resolve the operating system path.
5. Try to use more static HTML pages and less scripts.
The time for Apache to parse a PHP script is 2 to 10 times slower than parsing a static HTML page.
Recommended tutorial: PHP video tutorial
The above is the detailed content of Is php very inefficient?. For more information, please follow other related articles on the PHP Chinese website!