Search code examples
c++jsonboostboost-propertytree

performance is very bad to construct boost property tree from json file?


I am using boost property tree to load/dump json file. However, the performance is very bad.

For example, I have a json file whose size is 1.8M. The boost C++ program spends 3 seconds to load the json file and construct the property tree. If I use python to load the json file, it only need 0.1 second. And python will also construct everything as object as well.

The C++ program is like:

int main(int argc, char **argv){
        std::fstream fin;
        fin.open(argv[1], std::fstream::in);
        if (!fin.is_open()){
            ASSERT(false);
        }

        boost::property_tree::ptree pt;
        try{
            read_json(fin, pt);
        }catch(ptree_error & e) {
            ASSERT(false);
        }
        fin.close();

    return 0;
}

The python script which is doing same thing is like:

#!/usr/bin//python

import sys
import json

fp = open(sys.argv[1],"r")
objs = json.load(fp)

I tried the lastest boost (1.54). It's still very slow on doing this.

Appreciate for any advice.

If there is no solution, do you know any other C++ library to load/dump json?


Solution

  • It doesn't matter much what's really in the JSON file. I tried multiple JSON files with different conent. Boost is just slow.

    Now I already switched to jansson which is much better - fast and nice API to use.