Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Python Performance Lab: Sharpening Your Instincts

This is bwhitt7's fork.

A PyCon US 2026 hands-on tutorial. You optimize intentionally slow Python code
across three rounds plus a team challenge, measuring every change with
[CodSpeed](https://codspeed.io).
Expand Down
38 changes: 36 additions & 2 deletions rounds/3_dna/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,46 @@
"""

from .baseline import find_matches as _baseline
from concurrent.futures import ThreadPoolExecutor
import threading
import os

def _thread_worker(pattern_str: str, record: str, matches, lock:threading.Lock):
if not record.strip():
return
lines = record.split("\n")
record_id = lines[0].strip()
sequence = "".join(lines[1:]).replace(" ", "")
positions: list[int] = []
start = 0
while True:
pos = sequence.find(pattern_str, start)
if pos == -1:
break
positions.append(pos)
start = pos + 1

if positions:
with lock:
matches[record_id] = positions

def find_matches(fasta_path: str, pattern: bytes) -> list[tuple[str, list[int]]]:
"""Find every FASTA record whose sequence contains ``pattern``.

Returns ``[(record_id, [positions...]), ...]`` in file order.
"""
# TODO: remove this delegation and write your own implementation here.
return _baseline(fasta_path, pattern)
pattern_str = pattern.decode("ascii")
with open(fasta_path, "r") as f:
text = f.read()

matches: dict[str, list[int]] = {}

lock = threading.Lock()
with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
executor.map(lambda args: _thread_worker(*args),
[
(pattern_str, record, matches, lock)
for record in text.split(">")
])
matches = dict(sorted(matches.items()))
return [(k,v) for k,v in matches.items()]
Loading