Simple Loop Optimization
Basic example of optimizing a Python loop.
Before (Slow)
import timedef sum_squares(n):total = 0for i in range(n):total += i * ireturn totalstart = time.perf_counter()result = sum_squares(10_000_000)print(f"Time: {time.perf_counter() - start:.2f}s")# Output: Time: 2.34s
After (Fast)
import epochlyimport time@epochly.optimizedef sum_squares(n):total = 0for i in range(n):total += i * ireturn totalstart = time.perf_counter()result = sum_squares(10_000_000)print(f"Time: {time.perf_counter() - start:.2f}s")# Output: Time: 0.003s (780x faster)
What Happened
- Epochly detected a numerical loop pattern
- Applied JIT compilation (Level 2)
- Loop runs as compiled machine code
- Result verified against baseline