Home > Backend Development > PHP Tutorial > List of 54 Tips to Improve PHP Operational Efficiency_PHP Tutorial

List of 54 Tips to Improve PHP Operational Efficiency_PHP Tutorial

WBOY
Release: 2016-07-13 17:42:33
Original
712 people have browsed it

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变量)中存储的已知字符串长度。但是,由于strlen()是函数,多多少少会有些慢,因为函数调用会经过诸多步 骤,如字母小写化(译注:指函数名小写化,PHP不区分函数名大小写)、哈希查找,会跟随被调用的函数一起执行。在某些情况下,你可以使用isset() 技巧加速执行你的代码。

(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, means 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 perform the same optimizations, and there are a large number of Internet Service Providers (ISPs) and servers that do not have command optimizers installed.

35. It is not necessary to 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 decompose code into methods when you need to.

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 programs 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 please 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 the Select SQL statement and perform as few Insert and Update operations as possible (I was criticized for updating);

46. Use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, I wasted time that could have been written by writing a custom function. It is a matter of experience!);

47. Do not declare variables inside a loop, especially large variables: objects (this seems to be not just a problem in PHP, right?);

48. Try not to loop 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 instead of while and for loops;

51. Use single quotes instead of double quotes to quote strings;

52. "Replace i=i 1 with i =1. It conforms to the c/c habit and is more efficient";

53. Global variables should be unset()ed after use;

54. Use single quotes instead of double quotes to include strings. This will be faster. Because PHP will search for variables in strings surrounded by double quotes, single quotes will not. Note: only echo can do this, it is a "function" that can take multiple strings as parameters (Annotation: PHP Manual It is said that echo is a language structure, not a real function, so the function is enclosed in double quotes).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486051.htmlTechArticle1. If you can define a class method as static, try to define it as static, and its speed will increase by nearly 4 times. 2. The speed of $row[id] is 7 times that of $row[id]. 3. echo is faster than print, and...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template