环境
华为云主机 1C1G docker php7 cli
测试代码
<?php
function getElapsT($targS, $count){
$t1 = microtime(true);
$tmp = '';
for($i = 0; $i < $count; $i ++){
$tmp = md5($targS);
}
$t2 = microtime(true);
return ($t2 - $t1) * 1000 * 1000;
}
$elaps1 = getElapsT( str_repeat('hello',10), 1000);
echo "字符串长度: 50, 循环1000次 ; 用时: {$elaps1}微秒 \n";
$elaps1 = getElapsT( str_repeat('hello',10), 10000);
echo "字符串长度: 50, 循环10000次 ; 用时: {$elaps1}微秒 \n";
$elaps1 = getElapsT( str_repeat('hello',1000), 1000);
echo "字符串长度: 5000, 循环1000次 ; 用时: {$elaps1}微秒 \n";
$elaps1 = getElapsT( str_repeat('hello',1000), 10000);
echo "字符串长度: 5000, 循环10000次 ; 用时: {$elaps1}微秒 \n";
输出结果
字符串长度: 50, 循环1000次 ; 用时: 396.0132598877微秒
字符串长度: 50, 循环10000次 ; 用时: 3997.802734375微秒
字符串长度: 5000, 循环1000次 ; 用时: 9607.0766448975微秒
字符串长度: 5000, 循环10000次 ; 用时: 95220.08895874微秒