python dict 构造函数性能比较

from time import time

t1 = time()
{i: "%d" % i for i in range(5000)}
t2 = time()
print(t2-t1)
t1 = time()
dict((i, "%d" % i) for i in range(5000))
t2 = time()
print(t2-t1)

assert dict((i, 1) for i in range(10)) == {i: 1 for i in range(10)}

 

0.0015797615051269531 要快些
0.0018689632415771484

posted @ 2018-10-30 16:00  bonelee  阅读(698)  评论(0编辑  收藏  举报