PHP之字符串编码转换之ICONV与MB性能测试


$str = "想象在鼠标当前的位置和子菜单的左上角和左下角之间画一个三角形。如果鼠标在这个三角形的范围之内移动,那用户很有可能是在把鼠标从主菜单向子菜单里挪,所以不要立刻更新子菜单。但是如果鼠标挪动到这个三角形之外,则可以马上更新子菜单。这就是 Amazon 主页反应速度超快的下拉菜单背后的算法。";
//$str = substr($str, 0, 29) . iconv("UTF-8", "GBK", "好吧") . "ABC";

$loop = 100000;
$tset = "GBK";
$sset = "UTF-8";

echo iconv($sset, $tset, "   MB转换:");
$out = "";
$start = microtime(true);
for ($i = 0; $i < $loop; ++$i) {
    $out = mb_convert_encoding($str, $tset, $sset);
}
echo (microtime(true) - $start), "\n";
//echo $out, "\n";

echo iconv($sset, $tset, "ICONV转换:");
$out = "";
$start = microtime(true);
for ($i = 0; $i < $loop; ++$i) {
    $out = @iconv($sset, $tset, $str);
}
echo (microtime(true) - $start), "\n";


PHP 5.3.5 cli Ubuntu Server 11.04测试如下:

MB转换:1.3023591041565
ICONV转换:0.51363492012024
PHP 5.3.5 cli Windows 7 测试如下:

MB转换:3.1859760284424
ICONV转换:0.63508296012878
可见 ICONV 性能要比 MB 高不少。但 ICONV 的容错没有 MB 好。






你可能感兴趣的:(PHP之字符串编码转换之ICONV与MB性能测试)