66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Precomputed config validator - uses 2D array lookup.
|
|
Profile this to see clean array indexing with no wrapper overhead.
|
|
|
|
Usage:
|
|
python3 config_validator_precomputed.py
|
|
python3 -m cProfile -s tottime config_validator_precomputed.py
|
|
"""
|
|
|
|
import time
|
|
|
|
from common import validate_rule_slow, load_events, RULES, EVENT_TYPES
|
|
|
|
|
|
def build_validation_table():
|
|
"""
|
|
Build a 2D lookup table for all possible (rule_id, event_type) combinations.
|
|
Array indexing is faster than hash-based lookup because:
|
|
- No hash computation needed
|
|
- Direct memory offset calculation
|
|
- Better CPU cache locality
|
|
"""
|
|
table = []
|
|
for rule_id in range(max(RULES) + 1):
|
|
row = []
|
|
for event_type in range(max(EVENT_TYPES) + 1):
|
|
row.append(validate_rule_slow(rule_id, event_type))
|
|
table.append(row)
|
|
return table
|
|
|
|
|
|
# Build table at module load time (simulates startup initialization)
|
|
VALIDATION_TABLE = build_validation_table()
|
|
|
|
|
|
def process_events(events):
|
|
"""Process events using precomputed 2D lookup table."""
|
|
valid_count = 0
|
|
for rule_id, event_type, data in events:
|
|
if VALIDATION_TABLE[rule_id][event_type]:
|
|
valid_count += 1
|
|
return valid_count
|
|
|
|
|
|
ITERATIONS = 5
|
|
|
|
|
|
def main():
|
|
events = load_events()
|
|
print(f"Processing {len(events)} events (precomputed), {ITERATIONS} iterations...")
|
|
|
|
times = []
|
|
for _ in range(ITERATIONS):
|
|
start = time.perf_counter()
|
|
valid_count = process_events(events)
|
|
times.append(time.perf_counter() - start)
|
|
|
|
avg = sum(times) / len(times)
|
|
print(f"Valid: {valid_count}")
|
|
print(f"Avg time: {avg:.3f}s")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|