New submission from Terry J. Reedy:

Proposal from SO: in the iteration loop for 'myset.difference(iterable)', add 
equivalent of "if not myset: break'.
https://stackoverflow.com/questions/39378043/why-does-pythons-set-difference-method-take-time-with-an-empty-set

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 ;-):

1) averaged over all uses, the time saved not iterating would be less than the 
time spent testing emptyness.

2) an implicit guarantee to complete the iteration for possible side-effects.

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.

----------
messages: 275707
nosy: rhettinger, terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Stop set.difference when set is empty
type: performance
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28071>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to