My application makes extensive use of mb_ string functions and switching to php 7 resulted in an overall slower application. I tracked down the issues to the mb_ string functions. Here are the benchmark code and the results:
$time = microtime();
$time = explode(' ', $time);
$start = $time[1] + $time[0];
$startms = $time[0];
for ($i=0; $i<100000; $i++) {
$a = mb_strlen("fdsfdssdfoifjosdifjosdifjosdij:ά", "UTF-8");
}
$time = microtime();
$time = explode(' ', $time);
$finish = $time[1] + $time[0];
$finishms = $time[0];
$total_time = round(($finish - $start), 4);
echo "mb_strlen: " . $total_time*1000 ." milliseconds<br/>";
$time = microtime();
$time = explode(' ', $time);
$start = $time[1] + $time[0];
$startms = $time[0];
for ($i=0; $i<100000; $i++) {
$a = mb_stripos("fdsfdssdfoifjosdifjosdifjosdij:ά", "α", 0, "UTF-8");
}
$time = microtime();
$time = explode(' ', $time);
$finish = $time[1] + $time[0];
$finishms = $time[0];
$total_time = round(($finish - $start), 4);
echo "mb_stripos: " . $total_time*1000 ." milliseconds<br/>";
$time = microtime();
$time = explode(' ', $time);
$start = $time[1] + $time[0];
$startms = $time[0];
for ($i=0; $i<100000; $i++) {
$a = mb_substr("fdsfdssdfoifjosdifjosdifjosdij:ά", $i, 1, "UTF-8");
}
$time = microtime();
$time = explode(' ', $time);
$finish = $time[1] + $time[0];
$finishms = $time[0];
$total_time = round(($finish - $start), 4);
echo "mb_substr: " . $total_time*1000 ." milliseconds<br/>";
The platform is Windows 7 64bit, IIS 7.5:
php 5.3.28
mb_strlen: 250 milliseconds
mb_stripos: 3078.1 milliseconds
mb_substr: 281.3 milliseconds
php 7.1.1
mb_strlen: 406.3 milliseconds
mb_stripos: 4796.9 milliseconds
mb_substr: 421.9 milliseconds
I don't know if my set up is wrong or something, but seems inconceivable that the multibyte functions should be slower. Any ideas as to why and what to do to solve this ? Thank you in advance.
Edit: as apokryfos' comment suggests, this may be a Windows only problem.
microtime
takes a boolean argument that makes it return a float already - no need toexplode
etc. - Thinking about it: That might be the whole problem, what's$time = explode(' ', $time); $start = $time[1] + $time[0];
supposed to represent? You're just adding the msec part of the current timestamp to the seconds part? – ccKep Jul 11 '17 at 7:3766539800 1499759652
in that case - my bad. Point regarding readability stands ;-) – ccKep Jul 11 '17 at 10:14