三、JSON方面的改进
在Python 3.1中,json模块获得了一个C扩展,这使得它的性能有了显著的提高。以下程序将创建一个嵌套的数据结构,该数据结构由一个字典列表构成,而字典又包含其他存放基本值的字典的列表。此程序将整个列表串行化为一个JSON,然后又返回。下面给出了原始数据结构(重复了100次):
'z': [{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}],
'zzz': [{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}],
'zz': [{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}]}
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}],
'zzz': [{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}],
'zz': [{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'},
{'a': 1, 'c': 4.5599999999999996, 'b': 'BBBB'}]}
下面是处理清单 1 中的数据的程序:
from __future__ import with_statement
import sys
import time
import json
def test_json():
x = dict(a=1, b='BBBB', c=4.56)
x6 = 6 * [x]
y = dict(z=x6, zz=2 * x6, zzz=3 * x6)
print (y)
sys.exit()
o = 100 *[y]
start = time.time()
j = json.dumps(o)
assert json.loads(j) == o
end = time.time() - start
return end
test = test_json
if __name__=='__main__':
times = [test() for i in range(10)]
times.remove(max(times))
times.remove(min(times))
import sys
import time
import json
def test_json():
x = dict(a=1, b='BBBB', c=4.56)
x6 = 6 * [x]
y = dict(z=x6, zz=2 * x6, zzz=3 * x6)
print (y)
sys.exit()
o = 100 *[y]
start = time.time()
j = json.dumps(o)
assert json.loads(j) == o
end = time.time() - start
return end
test = test_json
if __name__=='__main__':
times = [test() for i in range(10)]
times.remove(max(times))
times.remove(min(times))
由于Python 2.5没有提供标准的json模块,所以下面是从Python 2.6、Python 3.0和Python 3.1中得到的结果:
* Python 2.6: 0.58422702550888062
* Python 3.0: 0.580562502146
* Python 3.1: 0.0455559492111
* Python 3.0: 0.580562502146
* Python 3.1: 0.0455559492111
这些结果表明,Python 2.6和Python 3.0之间没有明显的差别。Python 3.1的速度则提高了一个数量级。这一点非常重要,因为JSON是Web 服务的通用语,如果您的web服务会接收或返回大量的JSON数据的话,编码/解码工作将会占据处理每个请求的很大一部分时间。
还有另一个变化,就是json模块只能跟str(Python 3的Unicode字符串)一起使用,而不能跟字节一起使用。