开发者

php json_decoding vs xml parsing

What's 开发者_C百科faster? Any thoguhts/benchmarks?


json_decode() is faster. No discussion. However the margin can only be benchmarked on a specific XML document type. XML-RPC marshalling isn't that far off from JSON e.g. But anyway, you have to decide on what kind of data you want to transfer or save:

JSON is suitable for representation of scalar data types, and arrays or objects.

XML is foremost a document format family. You can use it to serialize data types from any programming language; but that's not its purpose. Think of XML as document micro databases.

So really it is an apples to books comparison.


@StaxMan: unscientific proof follows. Note how this example is already skewed in favour of JSON by using a suboptimal pseudo datastructure.

$json = <<<END
   [55, "text goes here", 0.1]
END;

$xml = <<<END
<array>
   <int>55</int>
   <string>text goes here</string>
   <float>0.1</float>
</array>
END;

for ($i=0,$t=t(); $i<100000; $i++) {
   json_decode($json);
}
echo "json ", t(-$t), "\n";

for ($i=0,$t=t(); $i<100000; $i++) {
   simplexml_load_string($xml);
}
echo "xml ", t(-$t), "\n";

function t($t1=0) {
   $a = explode(" ", microtime());
   return $a[0] + $a[1] + $t1;
}

Result:

json 1.6152667999268
xml 2.9058270454407

Again, very nothingsaying. But it's a theoretic advantage for JSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜