Documentation

Simple Loop Optimization

Basic example of optimizing a Python loop.

Before (Slow)

import time
def sum_squares(n):
total = 0
for i in range(n):
total += i * i
return total
start = 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 epochly
import time
@epochly.optimize
def sum_squares(n):
total = 0
for i in range(n):
total += i * i
return total
start = 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

  1. Epochly detected a numerical loop pattern
  2. Applied JIT compilation (Level 2)
  3. Loop runs as compiled machine code
  4. Result verified against baseline