只需运行下面的测试,就会发现填充
为什么?
OrderedDict()
比填充dict()
或list()
慢大约一个数量级。为什么?
# list()
In [1]: timeit test_list()
1000 loops, best of 3: 298 us per loop
# dict()
In [3]: timeit test_dict()
1000 loops, best of 3: 269 us per loop
# dict()
In [3]: timeit test_ord_dict()
100 loops, best of 3: 1.77 ms per loop
使用:
def test_ord_dict():
a = OrderedDict()
for el in xrange(1000):
a[el] = np.random.randint(100)
return a
def test_dict():
a = dict()
for el in xrange(1000):
a[el] = np.random.randint(100)
return a
def test_list():
a = list()
for el in xrange(1000):
a.append(np.random.randint(100))
return a
https://mail.python.org/pipermail/python-dev/2017-December/151283.html
。 - user136036