Skip to content

Instantly share code, notes, and snippets.

@fgregg
Created October 14, 2025 18:58
Show Gist options
  • Select an option

  • Save fgregg/b09ca99f32cf100da57648b250601314 to your computer and use it in GitHub Desktop.

Select an option

Save fgregg/b09ca99f32cf100da57648b250601314 to your computer and use it in GitHub Desktop.
simple bitset for evaluating performance
class BitSet:
def __init__(self):
self.bits = 0
def __len__(self):
return self.bits.bit_count()
def __and__(self, other):
result = BitSet()
result.bits = self.bits & other.bits
return result
def __str__(self):
return str(self.to_set())
def to_set(self):
return {i for i, bit in enumerate(bin(self.bits)[:1:-1]) if bit == "1"}
if __name__ == "__main__":
import random
import time
bitsets = []
psets = []
for _ in range(1000000):
bitset = BitSet()
bitset.bits = random.randint(0, 2**128)
bitsets.append(bitset)
psets.append(bitset.to_set())
start_time = time.perf_counter()
for a, b in zip(bitsets, bitsets[1:]):
result = len(a & b)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Execution time for bitset: {elapsed_time:.6f} seconds")
start_time = time.perf_counter()
for a, b in zip(psets, psets[1:]):
result = len(a & b)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Execution time for set: {elapsed_time:.6f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment