hydrobyte / McJSON

A Delphi / Lazarus / C++Builder simple and small class for fast JSON parsing.
MIT License
58 stars 20 forks source link

Slow json generation speed #14

Closed totyaxy closed 1 year ago

totyaxy commented 1 year ago

Presumably due to my not quite correct code. I create a json structure this way:

Json.Add(IndexStr);
for x in Tx do
  Json.Path(IndexStr).Add(x.key).AsString := McJsonEscapeString(x.value);   

This about look this way (about 150k line):

 "200": {
    "Key1": "Value1",
    "Key2": "Value2",
    "Key3": "Value3"
  },

 "201": {
    "Key1": "Value1",
    "Key2": "Value2",
    "Key3": "Value3"
  },

I think the low generation speed is because of the path usage, can this be fixed somehow? For example If I always write to the last one, the library doesn't need to search through the entire structure.

hydrobyte commented 1 year ago

Hi,

Try to leave the Path() call outside the loop. Actually, you don't need to call it:

JsonIdx := Json.Add(IndexStr);
for x in Tx do
  JsonIdx.Add(x.key).AsString := McJsonEscapeString(x.value);  

Could you report how fast is this modification (in % or seconds/milliseconds)?

Regards,

totyaxy commented 1 year ago

I knew it, there is a faster way than it, thank you!

Old method: 88,5 secs New method: 11.7 secs

It's about 800% faster way, thank you!

Maybe it's a good idea to write something similar in the wiki, because I think it's better read than "issues".

hydrobyte commented 1 year ago

OK

hydrobyte commented 1 year ago

Hi,

I've just commit a new project that might be of your interest:

https://github.com/hydrobyte/TestJSON

I'm planning to create a Lazarus version too.

Regards,

totyaxy commented 1 year ago

Yes, I am interested, but I can only run fpc/lazarus code. Maybe a deplhi that can be converted by lazarus.