rapidjson icon indicating copy to clipboard operation
rapidjson copied to clipboard

simple stringfy too slow, even slower than jsoncpp, dont know why

Open runner66 opened this issue 1 year ago • 5 comments

        rapidjson::StringBuffer xJsonBuffer;
        rapidjson::Writer<rapidjson::StringBuffer> xJson(xJsonBuffer);

        xJson.StartObject();

        strData.resize(0);
        strData.resize(400000, 'A');
   
        xJson.Key("data");
        xJson.String(strData.c_str(), strData.length(), kStringType);

        xJson.EndObject();

        std::string strRes = xJsonBuffer.GetString();

runner66 avatar Nov 23 '23 09:11 runner66

Are you using release configuration?

miloyip avatar Nov 23 '23 10:11 miloyip

Are you using release configuration?

Yes, without -g flag. I test them in the same project, with the same makefile setting.(with -O2 or without -O2, get the same result) At first, I use jsoncpp but find it too slow dealing with big string.Then I turn to rapidjson, but get slower result. I try to find anything wrong with my code, get nothing. Is there any advice for speedup?

runner66 avatar Nov 24 '23 01:11 runner66

I tried to replicate the results with your code:

(Not rapidjson::kStringType is incorrect, but this parameter does nothing for Writer)

#include "rapidjson/writer.h"
#include <iostream>
#include <string>

int main() {
    std::size_t s = 0;
    for (int i = 0; i < 1000; i++) {
        rapidjson::StringBuffer xJsonBuffer;
        rapidjson::Writer<rapidjson::StringBuffer> xJson(xJsonBuffer);

        xJson.StartObject();

        std::string strData;
        strData.resize(400000, 'A');

        xJson.Key("data");
        xJson.String(strData.c_str(), strData.length());

        xJson.EndObject();

        std::string strRes = xJsonBuffer.GetString();
        s += strRes.length();
    }
    std::cout << s << std::endl; // prevent compiler to optimize out everything
}
➜  rapidjson git:(master) ✗ g++ -I include test.cpp && time ./a.out 
400011000
./a.out  4.35s user 0.09s system 97% cpu
➜  rapidjson git:(master) ✗ g++ -O2 -I include test.cpp && time ./a.out
400011000
./a.out  0.95s user 0.06s system 80% cpu 1.265 total
➜  rapidjson git:(master) ✗ g++ -O3 -I include test.cpp && time ./a.out
400011000
./a.out  0.96s user 0.06s system 84% cpu 1.200 total

The results seem reasonable to me for generating 400MB data in a second.

With NEON on my machine:

➜  rapidjson git:(master) ✗ g++ -O2 -march=native -D RAPIDJSON_NEON -I include test.cpp && time ./a.out
400011000
./a.out  0.08s user 0.03s system 62% cpu 0.173 total

You may try on jsoncpp and make a comparison.

miloyip avatar Nov 24 '23 09:11 miloyip

TestRapidJson 400016000 time: 14062229 TestJsonCpp 400017000 time: 2006387 TestYYJson 400016000 time: 730904

the result is above, just as before. all tests are run in the same project with the same machine. And all are run without -O option as my origin setting。

runner66 avatar Nov 27 '23 02:11 runner66

We can only evaluate the performance with compiler optimization.

miloyip avatar Nov 30 '23 19:11 miloyip