[issue32945] sorted(generator) is slower than sorted(list-comprehension)

Antony Lee report at bugs.python.org
Sun Feb 25 04:30:03 EST 2018


Antony Lee <anntzer.lee at gmail.com> added the comment:

Feel free to close the issue if that's not the forum for this discussion, but I'm still baffled by what's happening.

In the example that you give, the first case needs to look up the `list` global and that callable (which happens to be the `list` type) needs to figure out what to do with generator passed in.  Indeed, we can compare

    In [22]: dis.dis(compile("[i for i in [1, 2]]", "<string>", "single"))
  1           0 LOAD_CONST               0 (<code object <listcomp> at 0x7f2d3237ec00, file "<string>", line 1>)
              2 LOAD_CONST               1 ('<listcomp>')
              4 MAKE_FUNCTION            0
              6 LOAD_CONST               5 ((1, 2))
              8 GET_ITER
             10 CALL_FUNCTION            1
             12 PRINT_EXPR
             14 LOAD_CONST               4 (None)
             16 RETURN_VALUE

In [23]: dis.dis(compile("list(i for i in [1, 2])", "<string>", "single"))
  1           0 LOAD_NAME                0 (list)
              2 LOAD_CONST               0 (<code object <genexpr> at 0x7f2d32392150, file "<string>", line 1>)
              4 LOAD_CONST               1 ('<genexpr>')
              6 MAKE_FUNCTION            0
              8 LOAD_CONST               5 ((1, 2))
             10 GET_ITER
             12 CALL_FUNCTION            1
             14 CALL_FUNCTION            1
             16 PRINT_EXPR
             18 LOAD_CONST               4 (None)
             20 RETURN_VALUE

Note how the latter has an extra function call (to `list`).

In the example I gave, however:

    In [24]: dis.dis(compile("sorted([i for i in [1, 2]])", "<string>", "single"))
    1           0 LOAD_NAME                0 (sorted)
                2 LOAD_CONST               0 (<code object <listcomp> at 0x7f2d3231eb70, file "<string>", line 1>)
                4 LOAD_CONST               1 ('<listcomp>')
                6 MAKE_FUNCTION            0
                8 LOAD_CONST               5 ((1, 2))
                10 GET_ITER
                12 CALL_FUNCTION            1
                14 CALL_FUNCTION            1
                16 PRINT_EXPR
                18 LOAD_CONST               4 (None)
                20 RETURN_VALUE

    In [25]: dis.dis(compile("sorted(i for i in [1, 2])", "<string>", "single"))
    1           0 LOAD_NAME                0 (sorted)
                2 LOAD_CONST               0 (<code object <genexpr> at 0x7f2d32328930, file "<string>", line 1>)
                4 LOAD_CONST               1 ('<genexpr>')
                6 MAKE_FUNCTION            0
                8 LOAD_CONST               5 ((1, 2))
                10 GET_ITER
                12 CALL_FUNCTION            1
                14 CALL_FUNCTION            1
                16 PRINT_EXPR
                18 LOAD_CONST               4 (None)
                20 RETURN_VALUE

so both cases are much more similar -- superficially, at least.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32945>
_______________________________________


More information about the Python-bugs-list mailing list