支持 gbk 编码的JSON 反序列化方案性能测试
对于前几篇文章: 支持gbk/gb2312/gb18030的PHP json扩展 , 在gbk/gb2312编码中如何使用json_encode/json_decode 提到的方案作了一下性能测试,测试的代码如下:
其中function.php 里面包含了前几篇文章提到的几个函数,json文件为具体的数据,数据量大约为5k:
<?php
include("function.php");
$f = file_get_contents("json");
echo "=================\n";
$from = microtime(true);
tb_json_decode_ex($f, true);
$to = microtime(true);
echo <<<HTML
\$from = microtime(true);
tb_json_decode_ex(\$f, true);
\$to = microtime(true);\n
HTML;
echo $to - $from;
echo "\n";
echo "=================\n";
$from = microtime(true);
tb_json_decode($f, true);
$to = microtime(true);
echo <<<HTML
\$from = microtime(true);
tb_json_decode(\$f, true);
\$to = microtime(true);\n
HTML;
echo $to - $from;
echo "\n";
echo "=================\n";
$from = microtime(true);
json_internal_encoding("GBK");
json_decode($f, true);
$to = microtime(true);
echo <<<HTML
\$from = microtime(true);
json_internal_encoding("GBK");
json_decode(\$f, true);
\$to = microtime(true);\n
HTML;
echo $to - $from;
echo "\n";
测试的结果如下:
=================
$from = microtime(true);
tb_json_decode_ex($f, true);
$to = microtime(true);
0.0027639865875244
=================
$from = microtime(true);
tb_json_decode($f, true);
$to = microtime(true);
0.0046548843383789
=================
$from = microtime(true);
json_internal_encoding("GBK");
json_decode($f, true);
$to = microtime(true);
0.00045919418334961
可以看到, 使用 扩展的效率大约比使用内置函数快5倍,而比使用PHP的递归函数要快10倍!