[Tutor] why is list.pop(0) slow?
Wari Wahab
wari@home.wari.org
Wed Jun 18 17:42:52 2003
I've been playing around with very large lists and found out by chance
that pop(0) is 8 times slower than pop(). Is it really that bad?
Performance improved when the list i have is reversed and used pop().
See this little snippet of code for example:
---
"""
This shows that poping from the back of the list is faster then the
beginning
of the list
"""
import profile
def generate(num = 2000):
return [[] for p in xrange(num)]
def popper(l):
while l:
l.pop()
def pooper(l):
while l:
l.pop(0)
if __name__ == '__main__':
print "###### Time taken to generate list"
profile.run('generate(50000)')
l = generate(50000)
print "###### Time taken to pop at the end of the list"
profile.run('popper(l)')
l = generate(50000)
print "###### Time taken to pop at the beginning of the list"
profile.run('pooper(l)')
"""
This prints:
###### Time taken to generate list
3 function calls in 0.260 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.040 0.040 0.230 0.230 <string>:1(?)
1 0.190 0.190 0.190 0.190 pooperpoper.py:7(generate)
1 0.030 0.030 0.260 0.260 profile:0(generate(50000))
0 0.000 0.000 profile:0(profiler)
###### Time taken to pop at the end of the list
3 function calls in 0.260 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.260 0.260 <string>:1(?)
1 0.260 0.260 0.260 0.260 pooperpoper.py:10(popper)
1 0.000 0.000 0.260 0.260 profile:0(popper(l))
0 0.000 0.000 profile:0(profiler)
###### Time taken to pop at the beginning of the list
3 function calls in 8.870 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 8.870 8.870 <string>:1(?)
1 8.870 8.870 8.870 8.870 pooperpoper.py:14(pooper)
1 0.000 0.000 8.870 8.870 profile:0(pooper(l))
0 0.000 0.000 profile:0(profiler)
"""
--
Regards: Wari Wahab
RoughingIT - http://roughingit.subtlehints.net
PyBlosxom - http://roughingit.subtlehints.net/pyblosxom