首页 > 编程 > PHP > 正文

PHP点运算符和双引号变量替换的性能比较

2020-03-22 17:10:27
字体:
来源:转载
供稿:网友
  • 【测试环境】

    windows 8.1

    wampserver 2.4

    总共循环10次,每次测试1000000次,计算出每次循环的时间和总的时间,并且算出平均值

    【测试代码】

    点运算符性能测试代码:

    $total = 1000000;$loop = 10;$a = '123';$b = '456';$c = '789';$str = '';$total_spend = 0;for ($num=1; $num<=$loop; $num++) {    $start = microtime(true);    for ($i=0; $i<$total; $i++) {        $str = $a . $b . $c;    }    $end = microtime(true);    $spend = $end - $start;    echo "loop $num: " . $spend .'<br/>';    $total_spend += $spend;}$avg = $total_spend / $loop;echo 'average spend: ' . $avg . '<br>';

    双引号变量替换性能测试代码:

    $total = 1000000;$loop = 10;$a = '123';$b = '456';$c = '789';$str = '';$total_spend = 0;for ($num=1; $num<=$loop; $num++) {    $start = microtime(true);    for ($i=0; $i<$total; $i++) {        $str = "{$a}{$b}{$c}";    }    $end = microtime(true);    $spend = $end - $start;    echo "loop $num: " . $spend .'<br/>';    $total_spend += $spend;}$avg = $total_spend / $loop;echo 'average spend: ' . $avg . '<br>';

    【测试结果】

    点运算符:

    wKiom1Mn7F2xOevsAADb5hTrWA8313.jpg

    双引号变量替换:

    wKiom1Mn7HbTpWzYAAD0rRKJkG0422.jpg

    【结论】

    在php中,双引号变量替换的性能比点运算符的性能要好。

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表