16

I'm building classes that interface with the Twitter API, and I'm wondering whether PHP's built-in XML or JSON parser is faster? Twitter will send me the same data in either format, so PHP performance will determine my choice. I'm using php_apc, so you can disregard parse time and assume I'm running off bytecode.

Thanks!

more: I'm just looking to get associative arrays from the data. I'm not doing tree walking, node iteration or anything too complex. The format will always be the same. (I hope!)

  • 7
    Why not benchmark it yourself on the real datasets you'll be using? Parser performance can be quite dependent on what you feed it. – Adam Wright Jun 14 '09 at 17:41
  • 1
    Which one of PHPs xml-apis do you plan to use? I would guess that SimpleXML and DOM is slower or on par with JSON, while XMLReader or SAX is faster. – Emil H Jun 14 '09 at 18:03

6 Answers6

17

The comment from Adam above convinced me to benchmark it. Using https://twitter.com/status/mentions.[format], I found that simplexml_load_string() is SLIGHTLY faster than json_decode(). But the difference is practically a margin of error.

Test #1 time (xml): 3.75221395493 seconds
Test #2 time (xml): 4.1562371254 seconds
Test #3 time (xml): 3.60420489311 seconds
Test #4 time (xml): 3.85622000694 seconds
Test #5 time (xml): 3.89622211456 seconds

versus

Test #1 time (json): 4.53225803375 seconds
Test #2 time (json): 4.06823205948 seconds
Test #3 time (json): 4.03222990036 seconds
Test #4 time (json): 3.80421590805 seconds
Test #5 time (json): 3.88022208214 seconds

on the following code (where I've already curl'ed the data to a file, data.[xml,json]).

<?php

$test = 'json';  //xml or json
$data = implode(file("data.".$test),"\r\n");

for ($t=1; $t<=5; $t++) {
    $start[$t] = microtime(1);
    for ($i=0; $i<3000; $i++) {
        if ($test == 'xml') $xml = simplexml_load_string($data);
        else $json = json_decode($data);
    }
    $end[$t] = microtime(1);
    echo "<p>Test #{$t} time ({$test}): " . ($end[$t] - $start[$t]). " seconds</p>";
}
  • 16
    This test however does not, after loading, read data from the XML object. While json_decode returns a simple array, SimpleXML gives an object with special methods to read the data, including some dynamic magic. This might impact the performance. – Bart van Heukelom Dec 13 '09 at 12:50
14

I didn't do any benchmark but...

Since JSON is only a description of nested string sequences, without the need to offer a DOM interface, attributes parsing and other subtle stuff, my guess is that a JSON parser is WAY faster that an XML parser.

Bite code
  • 578,959
  • 113
  • 301
  • 329
  • 1
    I agree. JSON has a much simpler and less dynamic structure. – Blixt Jun 14 '09 at 17:51
  • 4
    Parsing JSON format is much more complicated than well defined XML. Parsing string is very complicated job, maybe because you use standard library function you don't feel it, but is much simpler to parse structured XML than data structured in JSON. – Artem Barger Jun 14 '09 at 17:57
  • If you consider only pure parsing, JSON should be way faster. But it also depends on the use of the parsed data. – instanceof me Jun 14 '09 at 17:58
  • 13
    Uh. These are not correct answers -- correct one is "go measure it". Seriously. I have measured performances, and there is big overlap -- slow json parser is slower than fast xml parser and vice versa. Note: XML _PARSING_ is a very low-level thing that can be done fast (ditto for XML). DOM has nothing to do with parsing; it is a tree structure one can optionally build out of low-level tokens. Same for XML Schema which is fully optional. Using such add-ons has significant performance implications (overhead). Same is true for JSON add ons, if true data binding to actual objects is needed. – StaxMan Jun 14 '10 at 17:25
  • most benchmarks put it at 500 times faster in serializations at only a fifth of the cost in transport, results may vary - use as directed. – mike clagg Jun 27 '12 at 20:55
  • 3
    @mike - care to share a link with actual results? – rustyx Jan 10 '13 at 10:18
5

I tend to find that simplexml_load_string() is faster than json_decode() when the json return is an object.

However, having the json returned as an array using json_decode($string, true) is actually a lot faster than using an object (as is true with most things PHP when comparing arrays to objects).

Ive seen json_decode() being over twice as fast as simplexml_load_string() under these circumstances.

1

Have you considered how overall bandwidth savings might affect the performance of your script? If your requests are going to be repeated a significant amount of time using JSON should be the sound economic choice.

Salaryman
  • 395
  • 5
  • 13
1

The answer depends on how do you plan to use it. I mean if you will create some request and process them inside PHP script, I believe XML will be much faster. But once you considering to create and AJAX calls and later process of result you should consider using JSON, since you benefit from JavaScript automatic representation of JSON result as an objects as well it supports cross domain request with callback functions, whereas for XML you will proxy.

Artem Barger
  • 40,769
  • 9
  • 59
  • 81
1

JSON tends to be a lot smaller in size

Also you run json_decode once and then access the data as an array no use for any other functions.

Without running benchmarks id go with JSON is faster

Galen
  • 29,976
  • 9
  • 71
  • 89