Pra*_*tal 10 python json simplejson pickle
什么是更快:
(A)'Unpickling'(正在加载)一个酸洗字典对象,使用 pickle.load()
要么
(B)使用将JSON文件加载到字典 simplejson.load()
假设:在案例A中已存在pickle对象文件,并且在案例B中已存在JSON文件.
ale*_*cxe 21
速度实际上取决于数据,内容和大小.
但是,无论如何,让我们以json数据为例,看看速度更快(Ubuntu 12.04,python 2.7.3):
给这个JSON结构倾入test.json和test.pickle文件:
{
"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"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
测试脚本:
import timeit
import pickle
import cPickle
import json
import simplejson
import ujson
import yajl
def load_pickle(f):
return pickle.load(f)
def load_cpickle(f):
return cPickle.load(f)
def load_json(f):
return json.load(f)
def load_simplejson(f):
return simplejson.load(f)
def load_ujson(f):
return ujson.load(f)
def load_yajl(f):
return yajl.load(f)
print "pickle:"
print timeit.Timer('load_pickle(open("test.pickle"))', 'from __main__ import load_pickle').timeit()
print "cpickle:"
print timeit.Timer('load_cpickle(open("test.pickle"))', 'from __main__ import load_cpickle').timeit()
print "json:"
print timeit.Timer('load_json(open("test.json"))', 'from __main__ import load_json').timeit()
print "simplejson:"
print timeit.Timer('load_simplejson(open("test.json"))', 'from __main__ import load_simplejson').timeit()
print "ujson:"
print timeit.Timer('load_ujson(open("test.json"))', 'from __main__ import load_ujson').timeit()
print "yajl:"
print timeit.Timer('load_yajl(open("test.json"))', 'from __main__ import load_yajl').timeit()
Run Code Online (Sandbox Code Playgroud)
输出:
pickle:
107.936687946
cpickle:
28.4231381416
json:
31.6450419426
simplejson:
20.5853149891
ujson:
16.9352178574
yajl:
18.9763481617
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,unpickling via pickle并不是那么快 - cPickle如果你选择pickling/unpickling选项,那么定义就是你要走的路.ujson在这些特定数据上,这些json解析器看起来很有希望.
此外,json和simplejson库上加载更快pypy(见Python的JSON性能).
也可以看看:
请务必注意,您的特定系统,其他类型和数据大小的结果可能会有所不同.
| 归档时间: |
|
| 查看次数: |
7750 次 |
| 最近记录: |