简体   繁体   English

什么减少了PHP服务器的负载:SimpleXML或json_decode?

[英]What puts less load on a PHP server: SimpleXML or json_decode?

I'm starting to develop a web application in PHP that I hope will become incredibly popular and make me famous and rich. 我开始用PHP开发一个Web应用程序,我希望它会变得非常受欢迎并让我变得有名和丰富。 :-) :-)

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. 如果到了这个时候,我决定是使用SimpleXML将API的数据解析为XML还是使用json_decode,这可能会对应用程序的可伸缩性产生影响。

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 . 似乎json_decode执行速度比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. 我的结论是,我将暂时使用SimpleXML,因为它支持XPath表达式。

<?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

As the "lighter" format, I'd expect JSON to be slightly less stressful on the server, but I doubt it will be the biggest performance issue you find yourself dealing with as your site grows in popularity. 作为“更轻”的格式,我希望JSON在服务器上的压力稍微小一些,但我怀疑它会成为你自己处理的最大性能问题,因为你的网站越来越受欢迎。 Use whichever format you're more comfortable with. 使用您更熟悉的格式。

Alternatively, if you know how you'll be structuring your data, you could try making an XML-formatted version and a JSON-formatted version and just run it against your setup a few hundred thousand times to see if it makes a noticeable difference. 或者,如果您知道如何构建数据,则可以尝试制作XML格式的版本和JSON格式的版本,然后针对您的设置运行几十万次,看看它是否会产生明显的差异。

There's only one way to determine which is going to be easier on your server in your application with your data. 只有一种方法可以确定在您的应用程序中使用您的数据在服务器上哪个更容易。

Test it! 测试一下!

I'd generate some data that looks similar to what you'll be translating and use one of the unit testing frameworks to decode it a few thousand times using each of SimpleXML and json_decode, enough to get meaningful results. 我将生成一些看起来类似于您将要翻译的数据,并使用其中一个单元测试框架使用SimpleXML和json_decode对其进行几千次解码,足以获得有意义的结果。 And then you can tell us what worked. 然后你可以告诉我们什么有用。

Sorry this isn't exactly the sort of answer you were looking for, but in reality, it's the only right way to do it. 对不起,这不是你想要的那种答案,但实际上,这是唯一正确的方法。 Good luck! 祝好运!

Not really an answer to the question, but you could just wait until you have lots of users hitting your system. 这个问题并不是真正的答案,但您可以等到有很多用户访问您的系统。 You may be surprised where your bottlenecks actually lie: 您可能会对瓶颈实际存在的地方感到惊讶:

http://gettingreal.37signals.com/ch04_Scale_Later.php http://gettingreal.37signals.com/ch04_Scale_Later.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM