PHP性能开发-代码优化

工欲善其事必先利其器,作为一名合格的PHP开发工程师,应该尽可能的让自己的代码更加高效。本篇简单的提供了一些PHP代码优化方面的建议。旨在共同进步。

php经济性

经济性取决于PHP脚本,不同PHP脚本其经济性也会不同,简单而言就是尽量避免一些无效的函数调用,简化函数调用流程。因为函数调用越多,PHP脚本的响应时间就会越长。

提前计算循环长度

错误实例

<?php

$arr = array(1,2,3,4,5,6,7,8,9,10);

for($i = 0;$i<count($arr);$i++){
    echo $arr[$i];
}

首先我们来分析一下这段代码
初始化 索引变量 $i为0,并进行加1操作,期间通过 count() 检查数组长度,总共调用10次count,按经济性 调用越多,耗时越长的原则这是有问题的。总共这里有9次是属于多余调用。为此我们将其优化如下,优化后我们仅需调用一次count()

<?php

$arr = array(1,2,3,4,5,6,7,8,9,10);
$count = count($arr);
for($i = 0;$i<$count;$i++){
    echo $arr[$i];
}

优化完成后我们做一下性能对比、先看优化前代码执行占用时间(为了效果更明显,我们这里循环100万次)

<?php

$arr = array(1,2,3,4,5,6,7,8,9,10);
$begin_time = microtime(true);
for($j = 0;$j<100000;$j++)
{
    for($i = 0;$i<count($arr);$i++){
    }
}
echo(microtime(true) - $begin_time);

优化后的代码

<?php

$arr = array(1,2,3,4,5,6,7,8,9,10);
$count = count($arr);
$begin_time = microtime(true);
for($j = 0;$j<100000;$j++)
{
    for($i = 0;$i<$count;$i++){
    }
}
echo(microtime(true) - $begin_time);

分别执行十次计算平均值

类型平均耗时
循环前计算长度0.032001
循环中计算长度0.043003

可以看出在性能上还是有些许的提升

使用 foreach 代替 for while循环遍历数组元素

以下三段代码创建了一个包含100万元素的数组$items,每个元素的值都为12345678910,之后采用foreach,for,while循环遍历每个元素,然后分别统计出所需的消耗时间

foreach方式

<?php

$items = array_fill(0,1000000,'1234567910');
$begin_time = microtime(true);
foreach($items as $item){
    $x = $item;
}
echo microtime(true) - $begin_time;

while方式

<?php

$items = array_fill(0,1000000,'1234567910');
$begin_time = microtime(true);
$i = 0;
while($i<1000000){
    $x = $items[$i];
    $i++;
}
echo microtime(true) - $begin_time;

for

<?php

$items = array_fill(0,1000000,'1234567910');
$begin_time = microtime(true);

 for($i =0;$i<1000000;$i++){
     $x = $items[$i];
 }
echo microtime(true) - $begin_time;

性能比对

方法平均耗时
foreach0.018001
while0.035001
for0.034001

更快的访问对象

前言:
本节所书写的内容违背了常规面向对象编程逻辑,但由于在性能上可以得到较大的提升,所以在这里还是提一下。各位可根据自己项目的实际需要来决定如何利用。
以下为使用了封装的USER类,包含了name 和 age 私有成员变量。通过接口获取和设置对应的值。
同样,我们循环了百万次获取USER中的NAME值。以便接下来做性能对比

<?php

class User{
    private $name = "jiesen";
    private $age = '30';
    function getName(){
        return $this->name;
    }
    function getAge(){
        return $this->age;
    }
    function setName($name){
        $this->name = $name;
    }
    function setAge($age){
        $this->age = $age;
    }
}

$user = new \User();
$begin_time = microtime(true);
for($i = 0;$i<1000000;$i++){
    $user->getName();
}
echo microtime(true) - $begin_time;

以下代码为直接读取类的成员变量

<?php

class User{
    public $name = "jiesen";
    public $age = '30';
}

$user = new \User();
$begin_time = microtime(true);
for($i = 0;$i<1000000;$i++){
    $user->name;
}
echo microtime(true) - $begin_time;

性能对比

类型耗时
使用了封装0.108006
直接调用0.054003

先写到这,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯伪猿

觉得文章不错?打赏支持一下吧。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值