I'm starting to develop a web application in PHP that I hope will become incredibly popular and make me famous and rich. :-)
If that time comes, my decision whether to parse the API's data as XML with SimpleXML or to use json_decode could make a difference in the app's scalability.
Does anyone know which of these approaches is more efficient for the server?
Update: I ran a rudimentary test to see which method was more performant. It appears that json_decode
is slightly faster executing than simplexml_load_string
. This isn't terribly conclusive because it doesn't test things like the scalability of concurrent processes. My conclusion is that I will go with SimpleXML for the time being because of its support for XPath expressions.
<?php
$xml = file_get_contents('sample.xml');
$json = file_get_contents('sample.js');
$iters = 1000;
// simplexml_load_string
$start_xml = microtime(true);
for ($i = 0; $i < $iters; ++$i) {
$obj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
}
$end_xml = microtime(true);
// json_decode
$start_json = microtime(true);
for ($i = 0; $i < $iters; ++$i) {
$obj = json_decode($json);
}
$end_json = microtime(true);
?>
<pre>XML elapsed: <?=sprintf('%.4f', ($end_xml - $start_xml))?></pre>
<pre>JSON elapsed: <?=sprintf('%.4f', ($end_json - $start_json))?></pre>
Result:
XML elapsed: 9.9836
JSON elapsed: 8.3606