php性能优化少被注意的部分
2012-09-28 16:20
441 查看
下面内容来源于网络和Pro PHP Application Performance
1、使用require vs require_once
参考:http://www.laruence.com/2012/09/12/2765.html
require is faster than require_once due to the high number of operational stat calls
made when importing the PHP script. If the file you requested is located within the
directory /var/shared/htdocs/myapp/models/MyModels/ClassA.php , the operating system
will make a stat call within each of the directories before it reaches ClassA.php. In this
example, that is a total of six stat calls. While require also issues stat calls, they are fewer
in number. So, the fewer func tion calls there are, the fa ster your code becomes.
require比require_once 更快 是由于在导入php 脚本的时候的大量stat操作导致的。如果请求一个在/var/shared/htdocs/myapp/models/MyModels/ClassA.php这个地址的文件时,在达到ClassA.php前,操作系统将对每一个directories做一次stat调用。require_once需要6次调用。require也是有stat调用,但是在数量上更少,因此,使用它有更少的函数调用,也就更快
2、使用"comma"代替“period”
3、遍历数组,foreach最快,while和for次之
插曲:未优化代码
这个代码将count($items)移出来,减少函数调用和计算,效率更高
for,foreach,while代码,最高效的foreach,原因这里就不找了
4、fread在读小文件时更快,file_get_contents读大文件时更快,具体多大文件是中间线,自己可以根据环境测试下
5、读取类属性,将类属性设置为public,直接读取最快,原因就不说了,你懂的
1、使用require vs require_once
参考:http://www.laruence.com/2012/09/12/2765.html
require is faster than require_once due to the high number of operational stat calls
made when importing the PHP script. If the file you requested is located within the
directory /var/shared/htdocs/myapp/models/MyModels/ClassA.php , the operating system
will make a stat call within each of the directories before it reaches ClassA.php. In this
example, that is a total of six stat calls. While require also issues stat calls, they are fewer
in number. So, the fewer func tion calls there are, the fa ster your code becomes.
require比require_once 更快 是由于在导入php 脚本的时候的大量stat操作导致的。如果请求一个在/var/shared/htdocs/myapp/models/MyModels/ClassA.php这个地址的文件时,在达到ClassA.php前,操作系统将对每一个directories做一次stat调用。require_once需要6次调用。require也是有stat调用,但是在数量上更少,因此,使用它有更少的函数调用,也就更快
2、使用"comma"代替“period”
<?php echo "Hi "."there. "."how are "."you?"; //Slow echo "Hi ","there. ","how are ","you?"; //Faster…slightly
3、遍历数组,foreach最快,while和for次之
插曲:未优化代码
<?php $items = array(1,2,3,4,5,6,7,8,9,10); for($i=0; $i<count($items); $i++) { $x = 10031981 * $i; }
这个代码将count($items)移出来,减少函数调用和计算,效率更高
for,foreach,while代码,最高效的foreach,原因这里就不找了
<?php $items = array_fill(0, 100000, '12345678910'); $start = microtime(); reset($items); foreach($items as $item) { $x = $item; } echo microtime()-$start;
4、fread在读小文件时更快,file_get_contents读大文件时更快,具体多大文件是中间线,自己可以根据环境测试下
5、读取类属性,将类属性设置为public,直接读取最快,原因就不说了,你懂的
相关文章推荐
- 大家须知简单的php性能优化注意点
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- [转]LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- [转]LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- [转]LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- [转]LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP(转)
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP(原创整理来自IBM)
- 大家须知简单的php性能优化注意点
- [转]LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- LAMP 系统性能调优,第 2 部分: 优化 Apache 和 PHP
- 优化PHP性能实用技巧
- PHP性能优化 产生高度优化代码
- mysql数据库部分性能问题分析及优化
- PHP性能优化的五条技巧
- PHP性能之语言性能优化:安装VLD扩展——检测性能