[issue2672] speed of set.update(
Alexander Belopolsky
report at bugs.python.org
Thu Apr 24 21:29:35 CEST 2008
Alexander Belopolsky <belopolsky at users.sourceforge.net> added the comment:
On Thu, Apr 24, 2008 at 2:23 PM, John Arbash Meinel
<report at bugs.python.org> wrote:
..
> So if you compare consuming a generator multiple times to creating it
> each time, it is 0.662 usec - 0.173 usec = 0.489 usec to create a generator.
>
> So why does: "(i for i in l); x.update(y)" take an additional 1.208 usec.
>
> (I'm certainly willing to believe that set.update() is generator/list
> agnostic, but something weird is still happening.)
I've seen a similar strangeness in timings:
$ python -m timeit '(i for i in [])'
100000 loops, best of 3: 4.16 usec per loop
but
$ python -m timeit -s 'x = set()' 'x.update(i for i in [])'
1000000 loops, best of 3: 1.31 usec per loop
on the other hand,
$ python -m timeit -s 'x = []' 'x.extend(i for i in [])'
100000 loops, best of 3: 4.54 usec per loop
How can x.update(i for i in []) take *less* time than simply creating a genexp?
Note that there is no apparent bytecode tricks here:
1 0 LOAD_CONST 0 (<code object <genexpr> at
0xf7e88920, file "<stdin>", line 1>)
3 MAKE_FUNCTION 0
6 BUILD_LIST 0
9 GET_ITER
10 CALL_FUNCTION 1
13 RETURN_VALUE
>>> dis(lambda:x.update(i for i in []))
1 0 LOAD_GLOBAL 0 (x)
3 LOAD_ATTR 1 (update)
6 LOAD_CONST 0 (<code object <genexpr> at
0xf7e88920, file "<stdin>", line 1>)
9 MAKE_FUNCTION 0
12 BUILD_LIST 0
15 GET_ITER
16 CALL_FUNCTION 1
19 CALL_FUNCTION 1
22 RETURN_VALUE
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2672>
__________________________________
More information about the Python-bugs-list
mailing list