由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里

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数据测试】的更多相关文章

  1. SwingBench---ORACLE压力测试工具

    SwingBench---ORACLE压力测试工具 ◆描述SwingBench是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.5,基于JDK.该工具是免费的,可以在作者 ...

  2. 搭建和测试 Redis 主备和集群

    本文章只是自我学习用,不适宜转载. 1. Redis主备集群 1.1 搭建步骤 机器:海航云虚机(2核4GB内存),使用 Centos 7.2 64bit 操作系统,IP 分别是 192.168.10 ...

  3. 转 使用SwingBench 对Oracle RAC DB性能 压力测试

    ###########说明1: 1 Swingbench 简述 1.1 概述 这是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.2,最新版本2.3,基于JDK1.5.该 ...

  4. Jmh测试JDK,CGLIB,JAVASSIST动态代理方式的性能

    前言 JDK,CGLIB,JAVASSIST是常用的动态代理方式. JDK动态代理仅能对具有接口的类进行代理. CGLIB动态代理方式的目标类可以没有接口. Javassist是一个开源的分析.编辑和 ...

  5. 磁盘IO

    基本概念: 在数据库优化和存储规划过程中,总会提到IO的一些重要概念,在这里就详细记录一下,个人认为对这个概念的熟悉程度也决定了对数据库与存储优化的理解程度,以下这些概念并非权威文档,权威程度肯定就不 ...

  6. Python 手写数字识别-knn算法应用

    在上一篇博文中,我们对KNN算法思想及流程有了初步的了解,KNN是采用测量不同特征值之间的距离方法进行分类,也就是说对于每个样本数据,需要和训练集中的所有数据进行欧氏距离计算.这里简述KNN算法的特点 ...

  7. Node.js高级编程读书笔记 - 4 构建Web应用程序

    Outline 5 构建Web应用程序 5.1 构建和使用HTTP中间件 5.2 用Express.js创建Web应用程序 5.3 使用Socket.IO创建通用的实时Web应用程序 5 构建Web应 ...

  8. C语言初始化——bss段初始化、跃入C、C与汇编

    1.bss段初始化 变量 存放位置 初始化的全局变量 数据段 局部变量 栈 malloc函数分配的 堆 未初始的全局变量 bss段 说明:全局变量在未赋初值时,会被保留到bss段. 测试: #incl ...

  9. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

随机推荐

  1. 1.ehcache实现页面整体缓存和页面局部缓存

    转自:https://www.cnblogs.com/jianjianyang/p/4933016.html 好长时间没写博客了,真的是没时间啊.ps:其实就是懒!!接下来几篇要写下缓存,这里主要写下 ...

  2. Maven配置本地仓库

    当我们在myeclipse中update maven时可能会报错User setting file does not exist C:\Users\lenevo\.m2\setting.xml,以致于 ...

  3. 关于进程exit后,内存释放释放的实践

    最近碰到一个问题,或许也是小猿们都会碰到的问题:内存泄露. 都知道malloc后需要free才能释放内存,shmat后需要shmdt才能断掉内存区并使用IPC_RMID命令删除共享内存.那么如果是当前 ...

  4. Create a Basic Shader in Shader Forge

    [Create a Basic Shader in Shader Forge] 1.打开ShaderForge.Window-> Shader Forge.(打开速度较慢) 2.通过NewSha ...

  5. Python all() 函数

    Python all() 函数  Python 内置函数 描述 all() 函数用于判断给定的可迭代参数 iterable 中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False. ...

  6. Python help() 函数

    Python help() 函数  Python 内置函数 描述 help() 函数用于查看函数或模块用途的详细说明. 语法 help 语法: help([object]) 参数说明: object ...

  7. 使用rem的原理,62.5%,根据屏幕宽度等比压缩网页

    一.前言 我们在编写网页时,往往需要兼顾网页在不同屏宽情况下的显示 而有时为了省事,没时间写新的页面,也为了兼容考虑,这就需要使用等比压缩了 等比压缩的核心是rem 二.正文 (一).rem的使用   ...

  8. 6-关于#include<bits/stdc++.h>

    万能头文件#include<bits/stdc++.h> (转载)   最近在打cf时赛后翻阅别人的代码总是会发现一个陌生而奇怪的头文件#include<bits/stdc++.h& ...

  9. ROS 消息发布器和订阅器Publisher, Subscriber

    博客参考:https://www.2cto.com/kf/201705/639776.html 1.编写发布器节点节点(Node) 是指 ROS 网络中可执行文件.接下来,将会创建一个发布器节点(“t ...

  10. Condition Variable使用及其Thread Cancellation线程取消

    条件变量Condition Variable的一般用法: 唤醒用法: struct { pthread_mutex_t mutex; pthread_cond_t cond; //whatever v ...