Context Managers
Context managers for scoped optimization control.
optimize_context()
Apply optimization within a scope.
Signature
with epochly.optimize_context(level=None, **kwargs):# optimized code
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
level | int | None | Enhancement level (0-4) |
monitor | bool | True | Enable monitoring |
Example
import epochlywith epochly.optimize_context(level=3):result = heavy_computation(data)
monitoring_context()
Collect performance metrics within a scope.
Signature
with epochly.monitoring_context() as metrics:# monitored codeprint(metrics)
Returns (as context variable)
| Key | Type | Description |
|---|---|---|
duration_ms | float | Execution time |
cpu_time_ms | float | CPU time |
memory_mb | float | Memory used |
wait_time_ms | float | I/O wait time |
Example
import epochlywith epochly.monitoring_context() as metrics:result = process_data(large_dataset)print(f"Duration: {metrics.get('duration_ms')} ms")print(f"Memory: {metrics.get('memory_mb')} MB")
jit_context()
Apply JIT compilation within a scope.
Signature
with epochly.jit_context(backend='auto', **kwargs):# JIT-compiled code
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
backend | str | 'auto' | JIT backend |
cache | bool | True | Cache compiled code |
Example
import epochlywith epochly.jit_context(backend='numba'):result = numerical_computation(array)
threading_context()
Apply threading optimization within a scope.
Signature
with epochly.threading_context(max_workers=None):# threading-optimized code
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
max_workers | int | None | Maximum workers |
Example
import epochlywith epochly.threading_context(max_workers=16):results = [fetch(url) for url in urls]
full_optimize_context()
Apply maximum optimization within a scope.
Signature
with epochly.full_optimize_context(gpu=False, **kwargs):# fully optimized code
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
gpu | bool | False | Enable GPU |
workers | int | None | Worker count |
Example
import epochlywith epochly.full_optimize_context(gpu=True):result = matrix_computation(large_matrix)
benchmark_context()
Benchmark code within a scope.
Signature
with epochly.benchmark_context(name='benchmark', iterations=1) as results:# benchmarked code
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | 'benchmark' | Benchmark name |
iterations | int | 1 | Number of iterations |
warmup | int | 0 | Warmup iterations |
Returns (as context variable)
| Key | Type | Description |
|---|---|---|
mean_ms | float | Mean execution time |
min_ms | float | Minimum time |
max_ms | float | Maximum time |
std_ms | float | Standard deviation |
Example
import epochlywith epochly.benchmark_context("matrix_mult", iterations=100) as results:output = matrix_multiply(a, b)print(f"Mean: {results.get('mean_ms'):.2f} ms")
epochly_disabled_context()
Temporarily disable Epochly.
Signature
with epochly.epochly_disabled_context():# unoptimized code
Example
import epochly# Normal optimized executionresult1 = optimized_function(data)# Temporarily disable for comparisonwith epochly.epochly_disabled_context():result2 = optimized_function(data)
Nested Context Managers
Context managers can be nested:
import epochlywith epochly.monitoring_context() as outer_metrics:with epochly.optimize_context(level=3):result = computation(data)print(f"Total time: {outer_metrics.get('duration_ms')} ms")