Python-解析Json或XML哪个更快? [英] Python - Which is faster to parse Json or XML?

查看:396
本文介绍了Python-解析Json或XML哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的观察来看,总体上JSONParse的速度要快于XML.我发现了两个很好的问题.要求一个 PHP ,另一个要求JavaScript .我想了解python,python如何有效地处理它们?并且解析效率更高. 另外,请帮助选择最佳的XML XML解析器(例如xmlparser库,lxml或?)和JSON(simplejson,jsonlib或?).

From my observations overall JSON is faster to Parse than XML. I have found two good question regarding this. One is asked for PHP and other is asked for JavaScript. I want to know about python, how python is efficient with them? and which is more efficient to parse. Also please help in choosing the best Python parser for XML (e.g. xmlparser library , lxml or ?) and JSON (simplejson, jsonlib or ?).

推荐答案

在我看来,比较XML和JSON的解析时间没有任何意义.选择一种格式而不是另一种格式取决于您的用例.

In my opinion, it does not make sense to compare XML and JSON parsing times. Choosing one format over the other depends on your use case.

如果您只想以一种易于理解的简单格式存储JSON支持的原始类型,则可以使用JSON.如果您需要标记语言的所有功能和复杂性,请使用XML.您可能不想发明基于JSON的文档格式.

If you only want to store primitive types as supported by JSON in a simple, human-readable format, JSON is the way to go. If you need all the power and complexity of a markup language, use XML. You probably don't want to invent a document format based on JSON.

解析JSON和XML的瓶颈通常不是解析本身,而是数据的解释/表示.基于事件的XML解析器通常非常快,但是构建包含数千个小对象的复杂DOM树则不是.如果您需要将XML解析为嵌套的本机数据结构(例如列表和字典),则最慢的部分将是解析结果的解释,而不是实际的字符串分析.由于JSON可以解析这些原始类型而不是复杂的对象树,因此它可能会更快.

The bottleneck with parsing JSON and XML usually is not the parsing itself, but the interpretation/representation of the data. An event-based XML parser usually is very fast, but building a complex DOM tree of thousands of small objects is not. If you need to parse XML to nested native data structures such as lists and dictionaries, the slow part will be the interpretation of the parsing results, not the actual string analysis. Since JSON parses right to those primitive types rather than a complex object tree, it will likely be faster.

这篇关于Python-解析Json或XML哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆