优化PHP程序的方法

上传人:E**** 文档编号:118052683 上传时间:2019-12-11 格式:PDF 页数:5 大小:141.84KB
返回 下载 相关 举报
优化PHP程序的方法_第1页
第1页 / 共5页
优化PHP程序的方法_第2页
第2页 / 共5页
优化PHP程序的方法_第3页
第3页 / 共5页
优化PHP程序的方法_第4页
第4页 / 共5页
优化PHP程序的方法_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《优化PHP程序的方法》由会员分享,可在线阅读,更多相关《优化PHP程序的方法(5页珍藏版)》请在金锄头文库上搜索。

1、浪曦网-国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com 浪曦网-国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com 优化 PHP 程序的方法 该文档由国内顶级该文档由国内顶级 ITIT 视频网络编程教育平台整理发布视频网络编程教育平台整理发布 1. If a method c+an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法 可静态化,就对它做静态声明。速率可提升至 4 倍。2. echo is faster t.1. If a method c+

2、an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化,就 对它做静态声明。速率可提升至 4 倍。 2. echo is faster than print. echo 比 print 快。 3. Use echos multiple parameters instead of string concatenation. 使用 echo 的多重参数(译注: 指用逗号而不是句点)代替字符串连接。 4. Set the maxvalue for your for-loops before

3、and not in the loop. 在执行 for循环之前确定最大 循环数,不要每循环一次都计算最大值。 5. Unset your variables to free memory, especially large arrays. 注销那些不用的变量尤其是大 数组,以便释放内存。 6. Avoid magic like _get, _set, _autoload 尽量避免使用_get,_set,_autoload. 7. require_once() is expensive require_once()代价昂贵。 8. Use full paths in includes and

4、requires, less time spent on resolving the OS paths. 在包含文件 时使用完整路径,解析操作系统路径所需的时间会更少。 9. If you need to find out the time when the script started executing, $_SERVERREQUEST_TIME is preferred to time() 如果你想知道脚本开始执行(译注:即服务 器端收到客户端请求)的时刻,使用$_SERVERREQUEST_TIME要好于 time()。 10. See if you can use strncasec

5、mp, strpbrk and stripos instead of regex. 检查是否能用 strncasecmp,strpbrk,stripos 函数代替正则表达式完成相同功能。 11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. str_replace 函数比 preg_replace 函数快,但 strtr 函数的效率是 str_replace 函数的四倍。 12. If the function, such as string rep

6、lacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement 浪曦网-国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com 浪曦网-国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com statements, passing one character at a time, instead of one

7、 line of code that accepts arrays as search and replace arguments. 如果一个字符串替换函数,可接受数组或字符作为参数,并且参数长度 不太长,那么可以考虑额外写一段替换代码,使得每次传递参数是一个字符,而不是只写一行 代码接受数组作为查询和替换的参数。 13. Its better to use select statements than multi if, else if, statements. 使用选择分支语句(译 注:即 switch case)好于使用多个 if,else if 语句。 14. Error suppre

8、ssion with is very slow. 用屏蔽错误消息的做法非常低效。 15. Turn on apaches mod_deflate 打开 apache 的 mod_deflate 模块。 16. Close your database connections when youre done with them. 数据库连接当使用完毕时应 关掉。 17. $rowid is 7 times faster than $rowid. $rowid的效率是$rowid的 7 倍。 18. Error messages are expensive. 错误消息代价昂贵。 19. Do not

9、 use functions inside of for loop, such as for ($x=0; $x prop+)要比递增一个局部变量慢 3 倍。 23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递 增一个未预定义的局部变量要比递增一个预定义的局部变量慢 9 至 10 倍。 24. Just declaring a global variable without using it in a function also slows things

10、down (by about the same amount as incrementing a local var)。 PHP probably does a check to see if the global exists. 仅定义一个局部变量而没在函数中调用它, 同样会减慢速度(其程度相当于递增一个局部 变量)。PHP 大概会检查看是否存在全局变量。 25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more m

11、ethods to the test class (before and after the test method) with no change in performance. 方法调用看来与类中定义的方法的数量无关,因为我(在测试方法之前和之后都) 添加了 10 个方法,但性能上没有变化。 26. Methods in derived classes run faster than ones defined in the base class. 派生类中的方法运 行起来要快于在基类中定义的同样的方法。 浪曦网-国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com 浪曦网-

12、国内顶级 IT 视频网络编程教育平台 Www.LangSin.Com 27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar+ operations. A similar method call is of course about 15 $localvar+ operations. 调用带有一个参数的空函数,其花费的时间相当于执行 7 至 8 次的局部变量递增操作。类似的 方法调用所花费的时间接近于 15 次的局部变量递

13、增操作。 28. Surrounding your string by instead of will make things interpret a little faster since php looks for variables inside but not inside . Of course you can only do this when you dont need to have variables in the string. 用单引号代替双引号来包含字符串, 这样做会更快一些。 因为 PHP 会在双引号包围的字符串中搜寻变量,单引号则不会。当然,只有当你不需要在字符串中

14、包含 变量时才可以这么做。 29. When echoing strings its faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments. 输出多个字符串时, 用逗号代替句点来分隔字符串,速度更快。注意:只有 echo 能这么做,它是一种可以把多个字 符串当作参数的函数(译注:PHP 手册中说 echo 是语言结构,不是真正的函数,故把函数加上 了双引号)。

15、30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. Apache 解析一个 PHP 脚本的时间要比解析 一个静态 HTML 页面慢 2 至 10 倍。尽量多用静态 HTML 页面,少用脚本。 31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times. 除非脚 本可以缓存,否则每次调用时都会重新编译一次。引入一套 PHP 缓存机制通常可以提升 25%至 100%的性能,以免除编译开销。 32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号