Created
November 25, 2021 07:32
-
-
Save alxfv/5871d13ba8995e6c82486ffe631e819b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Author
alxfv
commented
Nov 25, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment