Delphi中的JSON与XML解析速度 [英] JSON vs XML parsing speed in Delphi

查看:588
本文介绍了Delphi中的JSON与XML解析速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在创建一个使用大量XML解析的应用程序,我认为也许使用JSON,因为我们可以使用JSON替代XML.

We are creating an application that uses a lot of XML parsing and I thought maybe to use JSON, because we can use JSON as an alternative to XML.

我需要测试使用哪种更快的JSON或XML,但是想在这里先征求意见.

I need to test which is faster JSON or XML, but thought to ask an opinion here first.

JavaScript中的JSON是本机的,而且速度很快.当然比解析XML更快.但是在Delphi中,没有这样做的本机类.

JSON in JavaScript is native and it's fast. Certainly faster than parsing XML. But in Delphi there's no native classes for doing that.

所以我的问题是-使用XML或JSON解析器库,在Delphi中哪个更快?还是它们的速度相等?

So my question is - which is faster in Delp using XML or JSON parser library? Or are they equal in speed?

如果速度更快,那么您更喜欢- Delphi Web Utils JSON Delphi库 JSON工具包

And if it's faster then which you prefer - Delphi Web Utils, JSON Delphi Library or JSON Toolkit

推荐答案

有了一个编写良好的解析器,XML和JSON或多或少都将具有相同的时间安排.您可以使用慢速JSON解析器和快速XML解析器.

With a well written parser, XML and JSON will have more or less the same timing. You can have a slow JSON parser, and a fast XML parser.

对于XML来说可能有点慢,因为语法比JSON更复杂.

Perhaps a bit slower for XML because the syntax is more complex than JSON's.

但是瓶颈将主要来自硬盘驱动器,而不是解析内容.

But the bottleneck will be mostly reading from the hard drive, not parsing the content.

出于某些原因,我们在ORM的客户端/服务器中使用了JSON(但是您会发现其他原因,我不想在这里进行拖曳,只是通过我们的小实验而已):

We used JSON for the Client/Server of our ORM, for several reasons (but you'll find others, I don't want to troll here, just speak from our little experiment):

  • 类似于XML,它是一种基于文本的,人类可读的格式,用于表示简单的数据结构和关联数组(称为对象);
  • (对于人类和机器而言)阅读起来更容易,实现起来更快,并且通常比XML的大小更小;
  • 这是一种非常有效的数据缓存格式;
  • 它的布局允许重写为单个零终止的UTF-8字符串,几乎没有浪费空间:此功能用于将表结果快速从JSON快速转换为文本,无需分配内存或复制数据;
  • JavaScript语言本身支持它,使其成为任何AJAX(即Web 2.0)应用程序中的理想序列化格式;
  • JSON格式是在众所周知的简单RFC中指定的;
  • JSON和我们的ORM的默认文本编码均为UTF-8,它允许存储和传达完整的Unicode字符集;
  • 这是自.NET Framework 3.5开始在Windows Communication Foundation(WCF)中创建的ASP.NET AJAX服务使用的默认数据格式;所以微软正式准备好了";
  • 对于二进制Blob传输,没有XML中的CDATA.因此,我们只需将JSON字符串内的二进制数据编码为十六进制或Base64(使用较少的空间)即可.

关于解析速度,您可以查看我们的 SQLite3结果中的就地解析器和JSON编写器.它对速度进行了非常优化,而且速度很快.我们为任何TPersistent(包括以下内容)编写了一个简单但高效的JSON序列化.集合.我们只需添加动态数组JSON序列化器,它也非常快.

About parsing speed, you could take a look at our in-place parser and JSON writer from SQLite3 results. It was very optimized for speed, and fast it is. We wrote a simple but efficient JSON serialization for any TPersistent, including collections. We just add a dynamic array JSON serializer, which is also very fast.

附加说明:

所有这些解析器都与您提到的解析器不同,因为它们解析JSON内容并将其格式化为输入缓冲区内的文本:解析期间没有进行内存分配,因此它应该比其他解决方案更快.文本内容未转义,字段以#0结尾,并计算了指向文本开头的指针.因此,要访问值,只需使用指针即可获取数据.通常,它会立即解析一些MB的JSON内容.

All those parsers differ from the one you mentioned because they parse the JSON content and format it as text inside the input buffer: there is no memory allocation made during parsing, so it should be faster than other solutions. Text content is unescaped, fields are #0 ended, and a pointer to the beginning of the text is computed. So to access a value, you just use the pointer to get the data. It usually parses some MB of JSON content with no time.

还可以查看DWS中嵌入的 JSON解析器.作者声称速度很快.但是仍然为每个对象分配了一个内存块.

Also take a look at the JSON parser embedded in DWS. The author claimed it was fast. But still allocated a memory block for each object.

这篇关于Delphi中的JSON与XML解析速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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