What's faster? Any thoguhts/benchmarks?
-
5That's comparing apples and oranges. If anything, the question should be which one is more suited for UseCase X? And when asking for benchmarks, why not do some your own?Gordon– Gordon2010-11-26 22:09:49 +00:00Commented Nov 26, 2010 at 22:09
-
Agree with Gordon, would have to know the case. But without any other info, I vote JSON. ;)Jason McCreary– Jason McCreary2010-11-26 22:12:40 +00:00Commented Nov 26, 2010 at 22:12
-
1possible duplicate of When to prefer JSON over XML?mario– mario2010-11-26 23:12:03 +00:00Commented Nov 26, 2010 at 23:12
-
Wrt "why not write your own" -- agreed, optimal results are gotten by testing one's own use case. But on the other hand there may exist well-written benchmarks done by experts who know of current best ways to handle XML and JSON; rolling your own runs the risk of novice mistakes.StaxMan– StaxMan2010-11-27 02:43:21 +00:00Commented Nov 27, 2010 at 2:43
1 Answer
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.