Posted on 2010-11-10 21:38
PHP博客 阅读(204)
评论(0) 编辑 收藏 引用 网摘
PHP本身的速度已经相当快了。一般来说,速度的瓶颈是等待数据库查询执行完成于从远程URL中提取内容等。由于Web平台的特点,PHP等web
脚本语言本身可能实现不了它应有的效率。在速度优化过程中增加更多的不定因素和复杂性。下面就介绍如何测试和发现代码中存在的性能问题的技术。Apache为Java向Oracle宣战
项目开始前,思维里应该有最基本的优化习惯并合理利用。项目的后期优化的成本就非常高了,可能你会重构某些代码块。下面讨论一些平时可以避免影响效率的习惯和方法。
功能相同的函数,谁的效率最高。这里有个经验,一般相同功能的函数,新版本的效率更高(注意PHP版本)。
滥用正则表达式(能满足需要的字符串函数代替正则表达式)。
平时对字符串、排序、数组方面研究积累。
需要说明一点,我们需要综合考虑函数在你的程序中被调用的次数、可读性、可维护性,如果只是为了省下微不足道的一小点时间,而增加了额外的复杂性就不值得了。
计算函数执行时间
07 |
$str = "netconcepts.cn" ; |
10 |
$start =microtime(true); |
14 |
$endtime =microtime(true) - $start ; |
15 |
echo 'Md5函数执行时间:' . $endtime ; |
计算程序执行时间-PEAR Benchmark 模块
timer_example.php
26 |
require 'Benchmark/Timer.php' ; |
35 |
function wait( $amount ) |
37 |
for ( $i =0; $i < $amount ; $i ++) { |
38 |
for ( $i =0; $i < 100; $i ++) { |
44 |
$timer = new Benchmark_Timer(); |
47 |
$timer ->setMarker( 'Mark1' ); |
48 |
echo "Elapsed time between Start and Mark1: " . |
49 |
$timer ->timeElapsed( 'Start' , 'Mark1' ) . "\n" ; |
设置几个记号
08 |
require 'Benchmark/Benchmark/timer.php' ; |
09 |
$timer =& new Benchmark_timer(true); |
12 |
$timer ->setMarker( 'setup' ); |
15 |
$timer ->setMarker( 'middle' ); |
18 |
$timer ->setMarker( 'done' ); |