PHP 关于字符串插值,和单引号字符串连接的性能测试。
一早上看了篇博客插值,做了下性能测试。发现有悖于常论。
<?php header("Content-type:text/html;charset=utf-8"); class test{ function getMillisecond() { list($s1, $s2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); } } $test = new test(); $array = null; $tempStr = "abc"; $start = $test->getMillisecond(); for ($i=0; $i < 10000000; $i++) { $array[$i] = 'abcd'.$tempStr.'!'; unset($array[$i]); } $end = $test->getMillisecond(); $res = $end-$start; echo '这是字符串拼接的所花时间'.$res; $array = null; $start = $test->getMillisecond(); for ($i=0; $i < 10000000; $i++) { $array[$i] = "abcd${tempStr}!"; unset($array[$i]); } $end = $test->getMillisecond(); $res = $end-$start; echo '这是插值所花时间'.$res; ?>
以上是测试代码。
测试结果是:
第一次: 这是字符串拼接的所花时间12203这是插值所花时间11227
第二次: 这是字符串拼接的所花时间12130这是插值所花时间11249
第三次: 这是字符串拼接的所花时间12127这是插值所花时间11217
.....
应该不是偶然。代码逻辑有问题么?能说明问题么?能当测试用例么?
积累知识,分享知识,学习知识。