In the previous posts I have shown you how to develop code that formats and parses arbitrary data types to and from JSON in C++ and Python:
- Here is the Python code
- Here is the C++ code
- And here is the repository containing both implementations
To my surprise you C++ implementation wasn't that much more complex than Python one, it is (to my greater surprise) also slightly more robust against future. In Python I needed to access private (and unmentioned in the documentation: __args__ and __origin__ attributes of the typing annotation, and my experience with typing module is that it does change, so while Python code is brittle and will require changes once new Python is released, C++ version should be future proof.
Performance comparison
C++ version is much faster than Python one. I have benchmarked both formatting and parsing json, both benchmarks did the same thing:
- Generate 10 different orders, and store them to a newline separated json file 500 000 times.
- Parse the aforementioned file checking how many times particular order occurs.
Performance for C++:
- Compile time: 6sec;
- Formatting runtime: 13sec (86mb/sec);
- Parsing runtime: 12sec;
Performance for Python:
- Formatting: 113sec (10mb/sec);
- Parsing: 164sec;