diff --git a/README.md b/README.md index 44e0723..f344a15 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Python Performance Lab: Sharpening Your Instincts +# Trey Perfomance Test A PyCon US 2026 hands-on tutorial. You optimize intentionally slow Python code across three rounds plus a team challenge, measuring every change with diff --git a/rounds/1_histogram/solution.py b/rounds/1_histogram/solution.py index dffbee5..0b71e61 100644 --- a/rounds/1_histogram/solution.py +++ b/rounds/1_histogram/solution.py @@ -6,9 +6,25 @@ """ +import numpy as np + def compute_histogram(path: str) -> dict[bytes, int]: """Frequency of every 2-byte bigram in the file at ``path``.""" - # TODO: remove this delegation and write your own implementation here. - from .baseline import compute_histogram as _baseline + data = np.fromfile(path, dtype=np.uint8) + + if len(data) < 2: + return {} + + bigrams = (data[:-1].astype(np.uint16) << 8) | data[1:] + counts = np.bincount(bigrams, minlength=65536) + + result = {} + nonzero_indices = np.nonzero(counts)[0] + + for idx in nonzero_indices: + byte1 = (idx >> 8) & 0xFF + byte2 = idx & 0xFF + result[bytes([byte1, byte2])] = int(counts[idx]) + + return result - return _baseline(path)