New submission from Alex Gaynor <alex.gay...@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@alex-gaynor-laptop:/tmp$ # Fresh 2.7 branch alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m6.418s user 0m6.408s sys 0m0.004s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.670s user 0m5.648s sys 0m0.008s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.688s user 0m5.672s sys 0m0.008s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.688s user 0m5.676s sys 0m0.004s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.690s user 0m5.684s sys 0m0.000s alex@alex-gaynor-laptop:/tmp$ alex@alex-gaynor-laptop:/tmp$ alex@alex-gaynor-laptop:/tmp$ # With patch alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m6.085s user 0m6.064s sys 0m0.008s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.728s user 0m5.720s sys 0m0.004s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m5.783s user 0m5.772s sys 0m0.004s alex@alex-gaynor-laptop:/tmp$ time ~/projects/cpython/python t.py real 0m4.730s user 0m4.716s sys 0m0.008s alex@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 <rep...@bugs.python.org> <http://bugs.python.org/issue14126> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com