只需运行下面的测试,就会发现填充 anOrderedDict()
大约比填充 adict()
或 a慢一个数量级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