PHP 代码优化测试【Benchmark数据测试】
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里
Benchmark
测试之前我们先来了解Benchmark。
直接下载:http://pear.php.net/package/Benchmark/download
Benchmark工具类包共有三个文件,分别是Timer.php、Iterate.php和Profiler.php,三个工具类功能相同,只是侧重点不同,都是用于调试代码获取程序的执行时间。
1,Benchmark_Timer类原理与通过microtime函数获取微秒时间再比较前后两个时间值的差相同。
2,Benchmark_Iterate类用于调试函数的平均执行时间。
3,Benchmark_Profiler类用于统计代码和函数的执行时间以及函数的调用次数。
我们用它来测试执行结果,使用它需要安装pear
1) $row['id'] =0比 $row[id]=0 快,次数越大越明显/生产环境(Linux)下测试1个数量级;
首先来测试直接写id的情况:
error_reporting(E_ALL&~E_NOTICE );
include "Benchmark/Timer.php";
header("Content-type: text/html; charset=utf-8"); require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate; function test(){
for($i = ;$i < ; $i++){
$arr[id] = ;
}
}
$bench->run(,"test");
echo '<pre>';
print_r($bench->get());
测试结果:
Array
(
[] => 0.140008
[] => 0.123007
[] => 0.117007
[] => 0.121007
[] => 0.114006
[] => 0.117007
[] => 0.112006
[] => 0.118007
[] => 0.114006
[] => 0.114007
[mean] => 0.119006
[iterations] =>
)
而当我们把$arr[id] = 0; 改为
$arr['id'] =
测试结果为:
Array
(
[] => 0.007001
[] => 0.008000
[] => 0.007001
[] => 0.009000
[] => 0.008000
[] => 0.007001
[] => 0.013001
[] => 0.008000
[] => 0.008001
[] => 0.012000
[mean] => 0.008700
[iterations] =>
)
可以看到,提升速度比较明显,因此,我们要规范数组的字段,不能写有风险的代码,$arr[id]这样子写有很大的风险。
2) 递增(递减)一个预预定义的局部变量要比递增(递减)一个未定义的局部变量快;差别较大
还是上述的代码,循环部分我们改为:
$arr[$i]++; //直接进行递增,没有预定义变量
执行耗时为:
Array
(
[] => 0.011001
[] => 0.011001
[] => 0.012000
[] => 0.012001
[] => 0.011001
[] => 0.015000
[] => 0.013001
[] => 0.017001
[] => 0.011001
[] => 0.014001
[mean] => 0.012700
[iterations] =>
)
而我们改为:
$arr[$i] = ; //进行了预定义变量
$arr[$i]++;
执行耗时:
Array
(
[] => 0.005000
[] => 0.003000
[] => 0.003001
[] => 0.003000
[] => 0.003000
[] => 0.003000
[] => 0.004000
[] => 0.003000
[] => 0.003001
[] => 0.004000
[mean] => 0.003400
[iterations] =>
)
耗时缩减挺多
3)在可行的情况下,避免使用正则表达式,str_replace 函数比 preg_replace,差别还是很明显的
首先来看使用正则的情况下:
error_reporting(E_ALL&~E_NOTICE );
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$str = "";
function test(){
for($i = ;$i < ; $i++){
$str = preg_replace('/0/','a',$str);
}
}
$bench->run(,"test");
echo '<pre>';
print_r($bench->get());
耗时:
Array
(
[] => 0.020001
[] => 0.019001
[] => 0.020001
[] => 0.017001
[] => 0.021001
[] => 0.019001
[] => 0.018001
[] => 0.017001
[] => 0.018001
[] => 0.021001
[mean] => 0.019001
[iterations] =>
)
而使用:
$str = str_replace('','a',$str);
耗时会减少挺多:
Array
(
[] => 0.006000
[] => 0.005000
[] => 0.005001
[] => 0.004000
[] => 0.004000
[] => 0.004000
[] => 0.004001
[] => 0.004000
[] => 0.004000
[] => 0.004000
[mean] => 0.004400
[iterations] =>
)
同样我们可以延伸出,尽量使用php的函数去完成功能,那些函数底层c都是经过优化的,执行效率比较高。即:尽量采用PHP内置函数,且选择效率高的函数
4)在有必要的时候使使用引用(&),测试差别较大,接近1个数量级
引用的话,就不用像正常传递变量那样,复制多一个变量, 而是直接使用地址即可。
我们先不用引用:
error_reporting(E_ALL&~E_NOTICE );
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$str = 'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww';
function setCon1($c){} function setCon2(&$c){} function test(){
for($i = ;$i < ; $i++){
setCon1($str); //这里使用setCon1,并没有用到引用
}
}
$bench->run(,"test");
echo '<pre>';
print_r($bench->get());
耗时:
Array
(
[] => 0.017001
[] => 0.017001
[] => 0.017001
[] => 0.016001
[] => 0.017001
[] => 0.016001
[] => 0.013001
[] => 0.012000
[] => 0.011001
[] => 0.014001
[mean] => 0.015000
[iterations] =>
)
而当我们在循环中改为:
setCon2($str);
耗时为:
Array
(
[] => 0.002001
[] => 0.001000
[] => 0.002000
[] => 0.002000
[] => 0.001000
[] => 0.004000
[] => 0.002000
[] => 0.002000
[] => 0.002000
[] => 0.001001
[mean] => 0.001900
[iterations] =>
)
时间上缩小了很多。
5 ) 判断字符串长度时,可用isset($str{15})代替strlen($str) < 15;因为isset()作为一种语言结构,而strlen()是函数,语言结构快于函数;
使用strlen($str)函数来判断,代码如下:
error_reporting(E_ALL&~E_NOTICE ); require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate; function test(){
$str = "Hello World";
for($i = ;$i < ; $i++){
strlen($str) < ;
}
} $bench->run(,"test");
echo '<pre>';
print_r($bench->get());
执行耗时:
Array
(
[] => 0.202800
[] => 0.140400
[] => 0.156001
[] => 0.140400
[] => 0.140400
[] => 0.156000
[] => 0.140401
[] => 0.140400
[] => 0.156000
[] => 0.140400
[mean] => 0.151320
[iterations] =>
)
而当我们使用
isset($str[]);
判断,将会加快很多,执行耗时:
Array
(
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.015600
[] => 0.000000
[] => 0.000000
[] => 0.000000
[mean] => 0.001560
[iterations] =>
)
6 ) $_SERVER['DOCUMENT_ROOT']代替str_replace('//','/',dirname(__FILE__) .'/') ; wamp测试无太大差别/Linux生产环境测试性能提升 500% (5倍)
首先我们先用 $path = str_replace('//','/',dirname(__FILE__) .'/'); 测试:
error_reporting(E_ALL&~E_NOTICE ); require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate; function test(){
for($i = ;$i < ; $i++){
$path = str_replace('//','/',dirname(__FILE__) .'/');
}
} $bench->run(,"test");
echo '<pre>';
print_r($bench->get());
执行耗时:
Array
(
[] => 0.320001
[] => 0.320000
[] => 0.320000
[] => 0.312002
[] => 0.322002
[] => 0.310000
[] => 0.310001
[] => 0.312006
[] => 0.322003
[] => 0.312002
[mean] => 0.316001
[iterations] =>
)
当改为:
$path = $_SERVER['DOCUMENT_ROOT'];
执行耗时:
Array
(
[] => 0.000000
[] => 0.010000
[] => 0.000000
[] => 0.000000
[] => 0.010000
[] => 0.000000
[] => 0.000000
[] => 0.010000
[] => 0.000000
[] => 0.000000
[mean] => 0.003000
[iterations] =>
)
几乎不在毫秒级别内。耗时少很多。
7 ) 获取Unix时间戳时用$_SERVER['REQUEST_TIME'] 代替time(); 测试性能提升很多
首先我们使用time() 来获取:
error_reporting(E_ALL&~E_NOTICE ); require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate; function test(){
for($i = ;$i < ; $i++){
$time = time();
}
} $bench->run(,"test");
echo '<pre>';
print_r($bench->get());
执行耗时:
Array
(
[] => 0.170001
[] => 0.150000
[] => 0.150000
[] => 0.150000
[] => 0.150001
[] => 0.150000
[] => 0.150000
[] => 0.150000
[] => 0.150000
[] => 0.160001
[mean] => 0.153000
[iterations] =>
)
而改为:
$time = $_SERVER['REQUEST_TIME'];
的时候,执行耗时减少很多:
Array
(
[] => 0.000000
[] => 0.000000
[] => 0.010001
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.000000
[] => 0.010000
[] => 0.000000
[] => 0.000000
[mean] => 0.002000
[iterations] =>
)
几乎不在毫秒级别内
其实还有很多其他的各种优化小细节,例如:
* foreach函数,没有用到键的时候,就不要加键。
* include 文件时尽量使用绝对路径,因为它避免了 PHP 去 include_path 里查找文件的速 度,解析操作系统路径所需的时间会更少。【测试
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
差别其实不明显】
* 用单引号(’’)代替双引号(””),单引号为强类型,将其中的所以字符都认作字符,而双引号的为弱类型,它会检测其中是否存在变量 【测试差别不大,但是用双引号有风险】
* Apache 处理 PHP 脚本的速度要比静态页面慢 2-10 倍,因此尽量采用多的静态页面,少的脚本;PHP程序使用文件缓存性能会倍增【不用测试我们也知道,测试速度快很多】;
* 一般不建议启用auto_start(session.auto_start:是否自动启用) ,因为创建Session需要消耗系统资源,我们通常只会在需要用到Sesson时,才会使用session_start函数来开启Session功能。
优化无止境.............................................................
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
PHP 代码优化测试【Benchmark数据测试】的更多相关文章
- SwingBench---ORACLE压力测试工具
SwingBench---ORACLE压力测试工具 ◆描述SwingBench是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.5,基于JDK.该工具是免费的,可以在作者 ...
- 搭建和测试 Redis 主备和集群
本文章只是自我学习用,不适宜转载. 1. Redis主备集群 1.1 搭建步骤 机器:海航云虚机(2核4GB内存),使用 Centos 7.2 64bit 操作系统,IP 分别是 192.168.10 ...
- 转 使用SwingBench 对Oracle RAC DB性能 压力测试
###########说明1: 1 Swingbench 简述 1.1 概述 这是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.2,最新版本2.3,基于JDK1.5.该 ...
- Jmh测试JDK,CGLIB,JAVASSIST动态代理方式的性能
前言 JDK,CGLIB,JAVASSIST是常用的动态代理方式. JDK动态代理仅能对具有接口的类进行代理. CGLIB动态代理方式的目标类可以没有接口. Javassist是一个开源的分析.编辑和 ...
- 磁盘IO
基本概念: 在数据库优化和存储规划过程中,总会提到IO的一些重要概念,在这里就详细记录一下,个人认为对这个概念的熟悉程度也决定了对数据库与存储优化的理解程度,以下这些概念并非权威文档,权威程度肯定就不 ...
- Python 手写数字识别-knn算法应用
在上一篇博文中,我们对KNN算法思想及流程有了初步的了解,KNN是采用测量不同特征值之间的距离方法进行分类,也就是说对于每个样本数据,需要和训练集中的所有数据进行欧氏距离计算.这里简述KNN算法的特点 ...
- Node.js高级编程读书笔记 - 4 构建Web应用程序
Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...
- C语言初始化——bss段初始化、跃入C、C与汇编
1.bss段初始化 变量 存放位置 初始化的全局变量 数据段 局部变量 栈 malloc函数分配的 堆 未初始的全局变量 bss段 说明:全局变量在未赋初值时,会被保留到bss段. 测试: #incl ...
- Redis Installation、Configuration、Program Based On Redis Learning
目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...
随机推荐
- SQLITE3 使用总结(转)
前序: Sqlite3 的确很好用.小巧.速度快.但是因为非微软的产品,帮助文档总觉得不够.这些天再次研究它,又有一些收获,这里把我对 sqlite3 的研究列出来,以备忘记. 这里要注明,我是一个跨 ...
- UNITY C#内存泄漏
http://www.360doc.com/content/15/0717/09/10504424_485422031.shtml
- git的基础操作-入门
本文是根据廖雪峰的git教程写的笔记:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...
- Attribute与元数据
在MSDN中,Attribute被定义为“是被指定给某一声明的一则附加的声明性信息”. 我们可以通过Attribute来定义设计层面的信息以及运行时(run-time)信息,也可以利用Attribut ...
- Cocoa Touch(四): 多线程GCD, NSObject, NSThread, NSOperationQueue
多线程的重要性不必多言,现代操作系统不可能离开进程线程的抽象.具体到ios应用,我们只能在一个进程中管理线程,主线程不应该去执行非常耗时间的后台操作导致出现卡机现象,后台的事情交给后台线程来完成. G ...
- [udemy]WebDevelopment_CSS
Your First CSS(Cascading Style Sheets) Cascading means it always takes selector that is at the end 即 ...
- Spring Boot 简单入门
添加相关依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- TableLayout 里的TextView等组的LayoutParams参数问题
TableLayout 里的TextView等组的LayoutParams参数不能是LinearLayout.LayoutParams这样来定义, 只能是用TableRow.LayoutParams ...
- 详解jQuery的$符号和init函数
本文所有代码,出自jQuery.1.5.2,为方便理解,引入类的概念,虽然jQuery不是基于面向对象思想. jQuery是现在最流行的JavaScript框架, $是其中最常见的符号,已经在jQue ...
- PHP5.2 $arr = [] 初始化数组出现问题
初始化数组 $arr=[] ,出现问题,使用 $arr = array() ,一切正常