New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop set.difference when set is empty #72258
Comments
Proposal from SO: in the iteration loop for 'myset.difference(iterable)', add equivalent of "if not myset: break'. In the toy example for testing that this is currently not so, myset starts empty, but it was noted by the OP that a more realistic example would be that myset becomes empty after deletions. Postulated reasons why not to do this (as opposed to why it has not been done yet ;-):
One answer notes that myset.difference(anotherset) is special-cased and faster than the equivalent non-set iterable. I searched the tracker for 'empty set difference' and got no hits. If I remember, I will post any disposition of this issue back to SO. |
This is reasonable, cheap, and not hard to do. I'll whip-up a patch shortly. |
New timings look nice: $ py -m timeit -s "r = range(10 ** 4); s = set()" "s.difference(r)"
10000000 loops, best of 3: 0.104 usec per loop
$ py -m timeit -s "r = set(range(10 ** 4)); s = set()" "s.difference(r)"
10000000 loops, best of 3: 0.105 usec per loop
$ py -m timeit -s "r = range(10 ** 4); s = set()" "s.difference_update(r)"
10000000 loops, best of 3: 0.0659 usec per loop
$ py -m timeit -s "r = set(range(10 ** 4)); s = set()" "s.difference_update(r)"
10000000 loops, best of 3: 0.0684 usec per loop |
New changeset fa0af1a6344d by Raymond Hettinger in branch 'default': |
The patch covers sets that start empty, but not sets that become empty during iteration. Are you thinking about or rejecting the latter? (And should this then be closed?) |
I only want a single quick check upfront. It doesn't make much sense to put this in the inner loop. |
This optimization caused a behavior change: Python 3.5: >>> set().difference([[]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list' Python 3.6: >>> set().difference([[]])
set() |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: