Skip to content

Instantly share code, notes, and snippets.

@hellais
Created January 18, 2016 18:55
Show Gist options
  • Save hellais/23cbf5776970c765b936 to your computer and use it in GitHub Desktop.
Save hellais/23cbf5776970c765b936 to your computer and use it in GitHub Desktop.
Benchmarking of YAML vs JSON parsing
#!/usr/bin/env python
#
# Running of the script against the following files (OONI reports):
# 87M 2015-12-22.json
# 97M 2015-12-22.yaml
#
# vanilla json: 1.37932395935
# ultra json: 0.421966075897
# simple json: 1.23581790924
# yaml: 193.864903927
# cyaml: 4.40925312042
import time
import json
import sys
target_file = sys.argv[1]
target_file_data = []
target_file_data_yaml = []
with open(target_file) as f:
for line in f:
target_file_data.append(line.strip())
with open(target_file.replace(".json", ".yaml")) as f:
entry = ""
for line in f:
if line.startswith("---"):
continue
if line.startswith("..."):
target_file_data_yaml.append(entry)
entry = ""
else:
entry += line
def benchmark_vanilla_json():
start_time = time.time()
for line in target_file_data:
json.loads(line)
return time.time() - start_time
def benchmark_ujson():
import ujson
start_time = time.time()
for line in target_file_data:
ujson.loads(line)
return time.time() - start_time
def benchmark_simplejson():
import simplejson
start_time = time.time()
for line in target_file_data:
simplejson.loads(line)
return time.time() - start_time
def benchmark_yaml():
import yaml
start_time = time.time()
for line in target_file_data_yaml:
yaml.load(line)
return time.time() - start_time
def benchmark_cyaml():
import yaml
from yaml import CLoader
start_time = time.time()
for line in target_file_data_yaml:
yaml.load(line, loader=CLoader)
return time.time() - start_time
print "vanilla json: %s" % benchmark_vanilla_json()
print "ultra json: %s" % benchmark_ujson()
print "simple json: %s" % benchmark_simplejson()
print "yaml: %s" % benchmark_yaml()
print "cyaml: %s" % benchmark_cyaml()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment