PHP: is JSON or XML parser faster?

后端 未结 6 2000
暗喜
暗喜 2020-12-06 16:36

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 eit

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 17:27

    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]).

    Test #{$t} time ({$test}): " . ($end[$t] - $start[$t]). " seconds

    "; }

提交回复
热议问题