PHP efficient writing method recommended_PHP tutorial

WBOY
Release: 2016-07-21 15:41:13
Original
789 people have browsed it

0、用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说echo是语言结构,不是真正的函数,故把函数加上了双引号)。
1、如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。

2、$row['id'] 的速度是$row[id]的7倍。

3、echo 比 print 快,并且使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2。

4、最好使用Foreach执行循环,在执行for循环之前确定最大循环数,不要每循环一次都计算最大值。

5、注销那些不用的变量尤其是大数组,以便释放内存。

6、尽量避免使用__get,__set,__autoload。对于__开头的函数就命名为魔术函数,此类函数都在特定的条件下厨房的。总得来说,有下面几个魔术函数

__construct(),__destruct(),__get(),__set(),__unset(),__call(),__callStatic(),__sleep(),__wakeup(),__toString(),__set_state(),__clone(),__autoload()

7、require_once()代价昂贵常用的避免require/include的方法

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. When operating a string and need to check whether its length meets certain requirements, you will naturally use the strlen() function. This function executes quite quickly because it does not do any calculations and just returns the known length of the string stored in the zval structure (C's built-in data structure used to store PHP variables). 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, 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 do the same optimizations, and there are a large number of Internet Service Providers (ISPs) and servers that don't have command optimizers installed.

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 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 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 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 a custom function, a matter of experience!);

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

48. Try not to loop and nest 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 loop;

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 after use;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321241.htmlTechArticle0. Use single quotes instead of double quotes to include strings, which will be faster. Because PHP will search for variables in the string surrounded by double quotes, single quotes will not. Note: only ech...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!