Skip to content

Instantly share code, notes, and snippets.

@alxfv
Created November 25, 2021 07:32
Show Gist options
  • Save alxfv/5871d13ba8995e6c82486ffe631e819b to your computer and use it in GitHub Desktop.
Save alxfv/5871d13ba8995e6c82486ffe631e819b to your computer and use it in GitHub Desktop.
from sys import getsizeof
from timeit import timeit
bitset = 0
N = 100000
def populate_bitset():
global bitset
for i in range(N):
bitset |= 1 << i
justset = set()
def populate_set():
for i in range(N):
justset.add(i)
populate_set()
populate_bitset()
print('bitset size =', getsizeof(bitset))
print('set size =', getsizeof(justset))
print('bitset time =', timeit('populate_bitset()', globals=globals(), number=100))
print('set time =', timeit('populate_set()', globals=globals(), number=100))
print('set/bitset ratio =', getsizeof(justset) / getsizeof(bitset))
@alxfv
Copy link
Author

alxfv commented Nov 25, 2021

bitset size = 13360
set size = 4194536
bitset time = 10.460648437
set time = 1.0610516790000002
set/bitset ratio = 313.9622754491018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment