The frozenset and set implementations frozenset mostly separate; a set is simply a frozenset with added mutation methods with the same hash table implementation. See Source Objects/setobject.c ; The top-level PyFrozenSet_Type definition uses the PySet_Type functions.
There is no optimization for frozenset, since there is no need to calculate hashes for elements in frozenset when you test membership. The element that you are using to test against the set still needs to calculate its hash to find the correct slot in the hashtable set so that you can run the equality test.
Thus, your synchronization results are probably disabled due to other processes running on your system; you measured the wall clock time and did not turn off the Python garbage collection, and you have repeatedly tested the same thing.
Try the test using the timeit module with one value from numbers and one of them is not installed:
import random import sys import timeit numbers = [random.randrange(sys.maxsize) for _ in range(10000)] set_ = set(numbers) fset = frozenset(numbers) present = random.choice(numbers) notpresent = -1 test = 'present in s; notpresent in s' settime = timeit.timeit( test, 'from __main__ import set_ as s, present, notpresent') fsettime = timeit.timeit( test, 'from __main__ import fset as s, present, notpresent') print('set : {:.3f} seconds'.format(settime)) print('frozenset: {:.3f} seconds'.format(fsettime))
This repeats each test 1 million times and produces:
set : 0.050 seconds frozenset: 0.050 seconds
Martijn pieters
source share