notes-python icon indicating copy to clipboard operation
notes-python copied to clipboard

list和tuple的遍历速度问题

Open jsonvin opened this issue 8 years ago • 2 comments

In [4]: %timeit for row in lst: list(row) 100 loops, best of 3: 4.12 ms per loop In [5]: %timeit for row in tup: tuple(row) 100 loops, best of 3: 2.07 ms per loop

这个地方有个疑问,我理解的话,这里差距的产生和list(row)与tuple(row)的速度有关吧?实际遍历的速度应该是差不多吧

jsonvin avatar Apr 25 '17 09:04 jsonvin

In [1]:
from numpy.random import rand
values = rand(10000,4)
lst = [list(row) for row in values]
tup = tuple(tuple(row) for row in values)
a=1

In [2]: %timeit for row in lst: b=a+1
1000 loops, best of 3: 381 µs per loop

In [3]: %timeit for row in tup: b=a+1
1000 loops, best of 3: 368 µs per loop

实际测试可以发现,你的理解应该是对的

zceng avatar Apr 25 '17 16:04 zceng

yes,your're right

liming0517 avatar May 21 '18 06:05 liming0517