4 ms·
The speedup is mostly because of using a faster library, not because of the language. I used py2many, a transpiler that can handle many simple language constru
by adsharma 2y ago
The speedup is mostly because of using a faster library, not because of the language.
I used py2many, a transpiler that can handle many simple language constructs and then modified the calls to json library to something that works.
The hard part of writing such utilities in python and getting a small, faster binary is library call translation. For this, py2many provides a framework. But someone needs to write library to library mappings.
Recently, py2many added a backend for mojo. It's very early in the game. But if mojo provides a python compatible stdlib, it becomes a lot more interesting as a backend.
Code here:
https://github.com/adsharma/json_parsing_langbench https://github.com/adsharma/json_parsing_langbench
- adsharma 2y agoEdit: The numbers have been revised by adding the missing -O3 The comment was based on what I saw by running an unoptimized C++ binary. You can repeat it yourself on your machine. But yeah - in general, C++ can be 70x faster than python on many benchmarks, which is why tools that translate between languages exist.
- adsharma 2y agoIncluding the output of py2many --mojo=1 test.py here for completeness. The generated code didn't run out of the box. Slightly modified below: from python import Python fn main() raises: var json = Python.import_module("json") for i in range(100000): var file = open("test.json", "r").read() var data = json.loads(file) var wealth_str = data["user"]["wealth"] var wealth = Float64(wealth_str) print("OK") Not much faster: # time -p ./test OK real 1.69 user 1.16 sys 0.40 But you get a small shippable binary: # du -sh test 4.1M test