Created
February 20, 2026 16:22
-
-
Save mbollmann/4a9a39a6930de00f152942e89b7f4344 to your computer and use it in GitHub Desktop.
Timing YAML vs. JSON parsing of the ACL Anthology people database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import msgspec | |
| import timeit | |
| import yaml | |
| from yaml import CLoader | |
| yaml_path = "../data/yaml/people.yaml" | |
| json_path = "../data/yaml/people.json" | |
| def load_yaml(): | |
| with open(yaml_path, "r", encoding="utf-8") as f: | |
| data = yaml.load(f, Loader=CLoader) | |
| return data | |
| def load_json(): | |
| with open(json_path, "rb") as f: | |
| data = msgspec.json.decode(f.read()) | |
| return data | |
| # Make sure JSON file exists and is equivalent to the YAML file | |
| with open(json_path, "wb") as f: | |
| f.write(msgspec.json.encode(load_yaml())) | |
| yaml_time = timeit.timeit("load_yaml()", globals=globals(), number=20) | |
| json_time = timeit.timeit("load_json()", globals=globals(), number=20) | |
| print(f"YAML loading took {yaml_time:2.2f}s") | |
| print(f"JSON loading took {json_time:2.2f}s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment