Home > Article > Backend Development > Methods to improve PHP programming efficiency_PHP tutorial
1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。
2、$row['id'] 的速度是$row[id]的7倍。
3、echo 比 print 快,并且使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2。
4、在执行for循环之前确定最大循环数,不要每循环一次都计算最大值,最好运用foreach代替。
5、注销那些不用的变量尤其是大数组,以便释放内存。
6、尽量避免使用__get,__set,__autoload。
7、require_once()代价昂贵。
8、include文件时尽量使用绝对路径,因为它避免了PHP去include_path里查找文件的速度,解析操作系统路径所需的时间会更少。
9、如果你想知道脚本开始执行(译注:即服务器端收到客户端请求)的时刻,使用$_SERVER[‘REQUEST_TIME']要好于time()。
10、函数代替正则表达式完成相同功能。
11、str_replace函数比preg_replace函数快,但strtr函数的效率是str_replace函数的四倍。
12、如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行代码接受数组作为查询和替换的参数。
13、使用选择分支语句(译注:即switch case)好于使用多个if,else if语句。
14、用@屏蔽错误消息的做法非常低效,极其低效。
15、打开apache的mod_deflate模块,可以提高网页的浏览速度。
16、数据库连接当使用完毕时应关掉,不要用长连接。
17、错误消息代价昂贵。
18、在方法中递增局部变量,速度是最快的。几乎与在函数中调用局部变量的速度相当。
19、递增一个全局变量要比递增一个局部变量慢2倍。
20、递增一个对象属性(如:$this->prop++)要比递增一个局部变量慢3倍。
21、递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。
22、仅定义一个局部变量而没在函数中调用它,同样会减慢速度(其程度相当于递增一个局部变量)。PHP大概会检查看是否存在全局变量。
23、方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都)添加了10个方法,但性能上没有变化。
24、派生类中的方法运行起来要快于在基类中定义的同样的方法。
25、调用带有一个参数的空函数,其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。
26、Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面,少用脚本。
27、除非脚本可以缓存,否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能,以免除编译开销。
28、尽量做缓存,可使用memcached。memcached是一款高性能的内存对象缓存系统,可用来加速动态Web应用程序,减轻数据库负载。对运算码 (OP code)的缓存很有用,使得脚本不必为每个请求做重新编译。
29、当操作字符串并需要检验其长度是否满足某种要求时,你想当然地会使用strlen()函数。此函数执行起来相当快,因为它不做任何计算,只返回在zval 结构(C的内置数据结构,用于存储PHP
The known length of the string stored in the variable). However, since strlen() is a function, it will be somewhat slow, because the function call will go through many steps, such as lowercase letters (Annotation: refers to the lowercase function name, PHP does not distinguish between uppercase and lowercase function names), hash search, Will be executed together with the called function. In some cases, you can use the isset() trick to speed up the execution of your code.
(Example below)
if (strlen($foo) < 5) { echo “Foo is too short”$$ }
(Compare with the following technique)
if (!isset ($foo{5})) { echo “Foo is too short”$$ }
Calling isset() happens to be faster than strlen(), because unlike the latter, isset(), as a language construct, Meaning that its execution does not require function lookup and letter lowercase. That is, you actually don't spend much overhead in the top-level code checking the string length.
34. When executing the increment or decrement of variable $i, $i++ will be slower than ++$i. This difference is specific to PHP and does not apply to other languages, so please don't modify your C or Java code and expect it to be instantly faster, it won't work. ++$i is faster because it only requires 3 instructions (opcodes), while $i++ requires 4 instructions. Post-increment actually creates a temporary variable that is subsequently incremented. Prefix increment increases directly on the original value. This is a form of optimization, as done by Zend's PHP optimizer. It's a good idea to keep this optimization in mind because not all command optimizers do the same optimizations, and there are a large number of Internet Service Providers (ISPs) and servers that don't have command optimizers equipped.
35. Not everything must be object-oriented (OOP), object-oriented is often very expensive, and each method and object call consumes a lot of memory.
36. It is not necessary to use classes to implement all data structures. Arrays are also very useful.
37. Don’t subdivide the methods too much. Think carefully about which code you really intend to reuse?
38. You can always break the code into methods when you need it.
39. Try to use a large number of PHP built-in functions.
40. If there are a large number of time-consuming functions in the code, you can consider implementing them using C extensions.
41. Profile your code. The checker will tell you which parts of the code take how much time. The Xdebug debugger includes inspection routines that evaluate the overall integrity of your code and reveal bottlenecks in your code.
42. mod_zip can be used as an Apache module to instantly compress your data and reduce data transmission volume by 80%.
43. When file_get_contents can be used instead of file, fopen, feof, fgets and other series of methods, try to use file_get_contents because it is much more efficient! But pay attention to the PHP version problem of file_get_contents when opening a URL file;
44. Conduct file operations as little as possible, although PHP’s file operation efficiency is not low;
45. Optimize Select SQL statements, when possible Under the circumstances, perform as few Insert and Update operations as possible (I was criticized on the update);
46. Use PHP internal functions as much as possible (but I wasted a lot of money just to find a function that does not exist in PHP. It takes less time to write a custom function, it’s a matter of experience! );
47. Don’t declare variables inside the loop, especially large variables: objects (this seems to be not just a problem in PHP, right?);
48. Try not to nest nested assignments in multi-dimensional arrays;
49. Do not use regular expressions when you can use PHP’s internal string manipulation functions;
50. Foreach is more efficient, try to use foreach replaces while and for loops;
51. Use single quotes instead of double quotes to quote strings;
52. "Use i+=1 instead of i=i+1. It conforms to the habits of c/c++ and is more efficient." ;
53. Global variables should be unset()ed when used.