温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-07 15:32:43 来源:网络 阅读:537 作者:自由de单车 栏目:web开发

【测试环境】

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>';


【测试结果】

点运算符:

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

双引号变量替换:

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


【结论】

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI