Skip to content

Instantly share code, notes, and snippets.

@juanarrivillaga
Last active March 27, 2017 22:00
Show Gist options
  • Save juanarrivillaga/3f07e5b7d2cd932dee1bd799c3bd31cc to your computer and use it in GitHub Desktop.
Save juanarrivillaga/3f07e5b7d2cd932dee1bd799c3bd31cc to your computer and use it in GitHub Desktop.
import timeit
import pickle
import json
s = """{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}"""
ob = json.loads(s)
with open('test.json', 'w') as f:
json.dump(ob, f)
with open('test.pickle', 'wb') as f:
pickle.dump(ob, f, protocol=pickle.HIGHEST_PROTOCOL)
def load_pickle(f):
return pickle.load(f)
def load_json(f):
return json.load(f)
print("pickle:")
print(timeit.Timer('with open("test.pickle", \'rb\') as f: load_pickle(f)', 'from __main__ import load_pickle').timeit())
print("json:")
print(timeit.Timer('with open("test.json") as f: load_json(f)', 'from __main__ import load_json').timeit())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment