一、参考我之前的博客,Delphi可以很方便的把类和结构体转换成JSON数据,但是数据量大了,就会非常之慢,1万条数据需要20秒左右。如果引用Serializers单元,那么100万数据只需要4秒左右,每秒处理20万+,速度还是很快的。
二、写一个简单的类
TPeople = class private FName: string; FScore: Integer; FAge: TDateTime; public property Name: string read FName write FName; property Score: Integer read FScore write FScore; property Age: TDateTime read FAge write FAge; end;
二、写个100万的大循环,创建100万个类实例,并把数据输出成JSON
uses REST.Json, System.Json, System.Types, System.Json.Types, System.Json.Writers, System.Json.Builders, System.Json.Serializers(*必须引用这个单元*); procedure TForm1.Button6Click(Sender: TObject); var m_People: TPeople; m_Time1, m_Time2: TDateTime; m_PeopleList: TArray<TPeople>; I: Integer; begin Memo1.Clear; Memo2.Clear; m_Time1 := Now; SetLength(m_PeopleList, 1000001); for I := 0 to 1000000 do begin m_PeopleList[I] := TPeople.Create; try // 循环赋值 m_PeopleList[I].Name := ‘张三‘; m_PeopleList[I].Age := Now; m_PeopleList[I].Score := I; finally end; end; TJsonSerializer.Create.Serialize(m_PeopleList); m_Time2 := Now; //看看100万个实例类生产JSON需要多久 Memo1.Lines.Add(FormatDateTime(‘ss.zzz‘, m_Time2 - m_Time1)); //把JSON显示出来 Memo2.Text := TJsonSerializer.Create.Serialize(m_PeopleList); end;
三、我们运行一下,看看结果:
本文为作者tianpan2019在博客园原创,未经许可,请勿转载。如果本文帮到了你,请记得打赏作者~感谢您的支持!
原文地址:https://www.cnblogs.com/tianpan2019/p/10498885.html
时间: 2024-11-13 18:43:26