[New-bugs-announce] [issue14126] Speed up list comprehensions by preallocating the list where possible

Alex Gaynor report at bugs.python.org
Sun Feb 26 00:29:27 CET 2012


New submission from Alex Gaynor <alex.gaynor at gmail.com>:

This adds a new opcode which for certain list comprehensions (ones with no if statements and only a single comprehension), preallocates the list to the appropriate size.

Patch is against 2.7, because it was a bit easier.  On:


def f():
    for i in range(10000):
        [j for j in range(10000)]

f()


Here's the speedup:


alex at alex-gaynor-laptop:/tmp$ # Fresh 2.7 branch
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m6.418s
user	0m6.408s
sys	0m0.004s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.670s
user	0m5.648s
sys	0m0.008s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.688s
user	0m5.672s
sys	0m0.008s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.688s
user	0m5.676s
sys	0m0.004s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.690s
user	0m5.684s
sys	0m0.000s
alex at alex-gaynor-laptop:/tmp$ 
alex at alex-gaynor-laptop:/tmp$ 
alex at alex-gaynor-laptop:/tmp$ # With patch
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m6.085s
user	0m6.064s
sys	0m0.008s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.728s
user	0m5.720s
sys	0m0.004s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m5.783s
user	0m5.772s
sys	0m0.004s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m4.730s
user	0m4.716s
sys	0m0.008s
alex at alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py 

real	0m4.691s
user	0m4.680s
sys	0m0.004s

----------
components: Interpreter Core
files: preallocate.diff
keywords: patch
messages: 154288
nosy: alex
priority: normal
severity: normal
status: open
title: Speed up list comprehensions by preallocating the list where possible
versions: Python 3.4
Added file: http://bugs.python.org/file24642/preallocate.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14126>
_______________________________________


More information about the New-bugs-announce mailing list