Python -哪个解析Json或XML更快?

时间:2021-12-01 02:54:18

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

从我的观察来看,JSON比XML更快地解析。关于这一点,我发现了两个很好的问题。一个是PHP,另一个是JavaScript。我想了解python, python是如何使用它们的?哪种解析更有效。还请帮助为XML(例如xmlparser library、lxml或?)和JSON (simplejson、jsonlib或?)选择最佳的Python解析器。

1 个解决方案

#1


8  

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.

在我看来,比较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支持的原始类型,那么使用JSON是可行的。如果您需要使用标记语言的所有功能和复杂性,请使用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.

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

#1


8  

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.

在我看来,比较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支持的原始类型,那么使用JSON是可行的。如果您需要使用标记语言的所有功能和复杂性,请使用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.

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