Performance comment (oddity with set.update())

Andrew Bennetts andrew at canonical.com
Wed Apr 23 07:13:49 BST 2008


Robert Collins wrote:
[...]
> We have a generator always. If we consume it with a list comprehension,
> it is faster than if we consume it via passing to x.update(). The latter
> example here does more work, but more quickly. This suggests a bug in
> update().
> 
> $ python -m timeit -s 'x = set(range(10000))' 'x.update(y for y in [])'
> 1000000 loops, best of 3: 0.837 usec per loop
> $ python -m timeit -s 'x = set(range(10000))' 'x.update([y for y in
> []])'
> 1000000 loops, best of 3: 0.462 usec per loop

No, there is no generator in the second example.

A more pithy comparison (with more dramatic differences in numbers) would be
"list(y for y in [])" vs "[y for y in []]".

-Andrew.




More information about the bazaar mailing list