You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For set s and set elem, the operations elem in s, s.remove(elem), and s.discard(elem) are equivalent to frozenset(elem) in s, s.remove(frozenset(elem)), and s.discard(frozenset(elem)), respectively. This feature is documented in Built-in Types:
Note, the elem argument to the contains(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, a temporary one is created from elem.
The implementation can be improved by calculating the hash value directly from the set object (as if it is a frozenset), instead of copying it into a new frozenset before calculating the hash value. The hash function for set is internal use only, and set is still unhashable.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
import timeit
for n in (1, 10, 100, 1000, 10000):
setup = f'''
a = set(range({n}))
b = {{frozenset(a)}}
'''
print(timeit.timeit("a in b", setup), f'N = {n}')
main branch result:
0.3688333749996673 N = 1
0.48004929200033075 N = 10
1.781020708000142 N = 100
11.256919416999153 N = 1000
152.03752795800028 N = 10000
my branch result:
0.30569541699878755 N = 1
0.3746081659992342 N = 10
1.2535620420021587 N = 100
7.2957435000025725 N = 1000
86.63279329200304 N = 10000
There will be no performance regression on the normal case.
Feature or enhancement
Proposal:
For set
s
and setelem
, the operationselem in s
,s.remove(elem)
, ands.discard(elem)
are equivalent tofrozenset(elem) in s
,s.remove(frozenset(elem))
, ands.discard(frozenset(elem))
, respectively. This feature is documented in Built-in Types:The implementation can be improved by calculating the hash value directly from the set object (as if it is a frozenset), instead of copying it into a new frozenset before calculating the hash value. The hash function for
set
is internal use only, andset
is still unhashable.Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
The text was updated successfully, but these errors were encountered: