Created
October 31, 2016 12:08
-
-
Save tgamblin/93ba38c3dbe099e7c979e71d75b49f11 to your computer and use it in GitHub Desktop.
Compare speed of YAML and JSON
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
#!/usr/bin/env spack-python | |
import os | |
import yaml | |
import json | |
import tempfile | |
import shutil | |
from timeit import timeit | |
import spack.util.spack_yaml as syaml | |
test_file = '/Users/gamblin2/src/spack/db/spack-db.features-db-locking.842c36c.2016-10-10-175746/spack/.spack-db/index.yaml' | |
#test_file = '/Users/gamblin2/src/spack/opt/spack/.spack-db/index.yaml' | |
#test_file = '/Users/gamblin2/.spack/cache/providers/builtin-index.yaml' | |
tmpdir = tempfile.mkdtemp() | |
yaml_file = os.path.join(tmpdir, 'file.yaml') | |
json_file = os.path.join(tmpdir, 'file.json') | |
#json_file = 'file.json' | |
with open(test_file) as yf: | |
data = syaml.load(yf) | |
# | |
# Write both files to the same location in tmpdir | |
# | |
with open(yaml_file, 'w') as yf: | |
syaml.dump(data, yf) | |
with open(json_file, 'w') as jf: | |
json.dump(data, jf, indent=True, separators=(',', ': ')) | |
# | |
# make sure round trip results in same data. | |
# | |
with open(yaml_file) as yf: | |
yaml_data = syaml.load(yf) | |
with open(json_file) as jf: | |
json_data = json.load(jf) | |
assert data == yaml_data | |
assert data == json_data | |
N = 10 | |
yaml_time = timeit( | |
'with open("%s") as f: syaml.load(f)' % yaml_file, | |
setup='import spack.util.spack_yaml as syaml', number=N) | |
print "YAML: ", yaml_time | |
json_time = timeit( | |
'with open("%s") as f: json.load(f, object_pairs_hook=OrderedDict)' % json_file, | |
setup='import json; from ordereddict_backport import OrderedDict', number=N) | |
print "JSON: ", json_time | |
print "Json speedup = %sx" % (yaml_time / json_time) | |
shutil.rmtree(tmpdir, ignore_errors=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment