Skip to main content

High-performance Python optimization toolkit with JIT compilation, variable specialization, and runtime optimizations

Project description

Python Optimizer 🚀

A high-performance Python optimization toolkit that provides JIT compilation, advanced variable specialization, intelligent caching, and runtime optimizations to accelerate Python code execution without changing language syntax.

Python 3.11+ Numba License: MIT Performance Thread Safe

🎯 Goal

Accelerate Python program execution by 10-500x through:

  • Advanced JIT compilation with Numba and custom optimizations
  • Intelligent variable specialization with type-aware caching
  • Adaptive optimization based on runtime patterns
  • Specialization caching with smart memory management
  • Zero syntax changes - works with existing Python code

⚡ Performance Results

Real-world performance improvements achieved:

Function Type Original Time Optimized Time Speedup Cache Hit Rate
Numerical Computation 2.06ms 0.04ms 51x 95%
Financial Metrics 100ms 2ms 50x 88%
Trading Simulation 500ms 5ms 100x 92%
Genetic Algorithm 30s 0.14s 214x -
Specialized Functions 1.2ms 0.003ms 400x 97%
Array Operations 50ms 0.1ms 500x 91%

Throughput: Up to 36,456 evaluations/second Cache Efficiency: 90%+ hit rates with intelligent eviction

🛠 Installation

# Clone the repository
git clone https://github.com/thinmanj/python-optimizer.git
cd python-optimizer

# Install with pip
pip install -e .

# Or install from PyPI (coming soon)
pip install python-optimizer

🚀 Quick Start

1. Basic JIT Optimization

from python_optimizer import optimize

@optimize(jit=True)
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

# First call compiles, subsequent calls are blazing fast
result = fibonacci(35)  # ~100x faster after compilation

2. Variable Specialization

from python_optimizer import optimize

@optimize(specialize=True, jit=False)
def adaptive_function(data):
    if isinstance(data, list):
        return sum(data)
    elif hasattr(data, '__len__'):
        return len(data)
    return data

# Automatically creates specialized versions for different types
result1 = adaptive_function([1, 2, 3, 4])      # List specialization
result2 = adaptive_function("hello world")      # String specialization  
result3 = adaptive_function(range(100))         # Range specialization
# Each type gets its own optimized version cached for future use

3. Financial Computing Example

import numpy as np
from python_optimizer.jit import calculate_sharpe_ratio_jit

# JIT-compiled financial metrics
returns = np.random.normal(0.001, 0.02, 252)  # Daily returns
sharpe = calculate_sharpe_ratio_jit(returns)   # ~50x faster

4. Trading Strategy Optimization

from python_optimizer.jit import JITBacktestFitnessEvaluator
from python_optimizer.genetic import Individual

# Ultra-fast backtesting with JIT compilation
evaluator = JITBacktestFitnessEvaluator(initial_cash=10000)
individual = Individual(genes={'ma_short': 10, 'ma_long': 30})

# Evaluate strategy performance
metrics = evaluator.evaluate(individual, market_data)
# Achieves 36,000+ evaluations per second

5. Advanced Caching & Monitoring

from python_optimizer import (
    get_specialization_stats, 
    clear_specialization_cache,
    configure_specialization
)

# Configure specialization behavior
configure_specialization(
    min_calls_for_specialization=3,
    enable_adaptive_learning=True,
    max_cache_size=1000
)

# Monitor performance
stats = get_specialization_stats()
print(f"Cache hit rate: {stats.get('cache_hit_rate', 0):.1%}")
print(f"Specializations created: {stats.get('specializations_created', 0)}")

📦 Features

Advanced JIT Compilation Engine

  • Numba-powered JIT compilation for numerical code
  • Automatic type inference and optimization
  • GIL-free execution for parallel processing
  • Intelligent caching system for compiled functions
  • Custom optimization passes for domain-specific code

Intelligent Variable Specialization

  • Type-aware specialization with automatic detection
  • Adaptive learning from runtime patterns
  • Memory-efficient specialized code paths
  • Multi-level caching with eviction policies
  • Thread-safe specialization cache
  • Performance monitoring and analytics

Advanced Caching System

  • Specialization cache with multiple eviction policies (LRU, LFU, Adaptive)
  • Memory-bounded cache with configurable limits
  • Weak references to prevent memory leaks
  • TTL-based expiration for temporal optimization
  • Thread-safe concurrent access
  • Real-time statistics and monitoring

Performance Profiling & Analytics

  • Runtime profiling with minimal overhead
  • Hot path detection and prioritization
  • Performance analytics and reporting
  • Specialization effectiveness tracking
  • Cache performance monitoring
  • Adaptive optimization recommendations

Financial Computing & Trading

  • JIT-optimized financial metrics (Sharpe ratio, drawdown, etc.)
  • Ultra-fast backtesting engine for trading strategies
  • Genetic algorithm optimization for parameter tuning
  • High-frequency trading optimizations
  • Portfolio optimization with risk management

📖 Documentation

Core Decorator

The @optimize decorator is the main entry point:

from python_optimizer import optimize

@optimize(
    jit=True,                    # Enable JIT compilation
    specialize=True,             # Enable variable specialization
    profile=True,                # Enable performance profiling
    aggressiveness=2,            # Optimization level (0-3)
    cache=True,                  # Enable specialization caching
    adaptive_learning=True,      # Enable adaptive optimization
    memory_limit_mb=100,         # Cache memory limit
    min_calls_for_spec=3         # Minimum calls before specialization
)
def your_function(x, y):
    # Your code here - automatically optimized based on usage patterns
    return x * y + compute_heavy_operation()

New Specialization Functions

from python_optimizer import (
    get_specialization_stats,
    clear_specialization_cache,
    configure_specialization,
    get_cache_stats
)

# Configure global specialization behavior
configure_specialization(
    min_calls_for_specialization=3,
    min_performance_gain=0.1,
    enable_adaptive_learning=True,
    max_cache_size=1000,
    max_memory_mb=100
)

# Get performance statistics
stats = get_specialization_stats('function_name')
print(f"Specializations created: {stats.get('specializations_created')}")
print(f"Cache hit rate: {stats.get('cache_hit_rate'):.2%}")
print(f"Performance gain: {stats.get('avg_performance_gain'):.2f}x")

# Global cache statistics
cache_stats = get_cache_stats()
print(f"Total cache entries: {cache_stats['total_entries']}")
print(f"Memory usage: {cache_stats['memory_usage_estimate']:.2f} MB")

# Clear cache when needed
clear_specialization_cache()  # Clear all
clear_specialization_cache('specific_function')  # Clear specific function

JIT Functions

Pre-built JIT-optimized functions:

from python_optimizer.jit import (
    calculate_returns_jit,
    calculate_sharpe_ratio_jit,
    calculate_max_drawdown_jit,
    simulate_strategy_jit
)

Genetic Algorithm Optimization

from python_optimizer.genetic import GeneticOptimizer, ParameterRange

# Define optimization parameters
param_ranges = [
    ParameterRange('learning_rate', 0.001, 0.1, 'float'),
    ParameterRange('hidden_layers', 1, 5, 'int'),
]

# Run optimization
optimizer = GeneticOptimizer(param_ranges, population_size=100)
best_params = optimizer.optimize(fitness_function, generations=50)

🧪 Examples

Check out the examples/ directory for:

  • Financial modeling with JIT optimization
  • Machine learning parameter optimization
  • Numerical computing acceleration
  • Trading strategy backtesting

📊 Benchmarks

Run benchmarks to see performance on your system:

python benchmarks/jit_performance_test.py
python benchmarks/genetic_algorithm_benchmark.py
python benchmarks/financial_metrics_benchmark.py

🔧 Configuration

Environment Variables

export PYTHON_OPTIMIZER_JIT_CACHE=1     # Enable JIT cache
export PYTHON_OPTIMIZER_PROFILE=1       # Enable profiling
export PYTHON_OPTIMIZER_PARALLEL=1      # Enable parallel execution

Configuration File

Create python_optimizer.toml:

[jit]
cache_dir = "~/.python_optimizer/cache"
compile_timeout = 30

[profiling]
enabled = true
output_dir = "./profiles"

[specialization]
max_variants = 5
threshold = 100

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

Development Setup

git clone https://github.com/thinmanj/python-optimizer.git
cd python-optimizer

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/

# Run linting
black python_optimizer/
isort python_optimizer/
flake8 python_optimizer/

📈 Roadmap

  • JIT Compilation Engine - Numba-based optimization
  • Advanced Variable Specialization - Type-aware optimization with caching
  • Intelligent Caching System - Multi-policy cache with memory management
  • Performance Monitoring - Real-time analytics and adaptive learning
  • Financial Computing Module - Trading strategy optimization
  • Genetic Algorithm - Parameter optimization
  • Thread-Safe Operations - Concurrent optimization support
  • GPU Acceleration - CUDA support for parallel execution
  • ML Model Optimization - PyTorch/TensorFlow integration
  • Distributed Computing - Multi-node optimization
  • Advanced Profiling - Visual performance analysis tools
  • Web Interface - Browser-based optimization dashboard

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Numba team for excellent JIT compilation framework
  • NumPy community for foundational numerical computing
  • Trading algorithm researchers for inspiration and validation

📞 Support


Star this repository if Python Optimizer helps accelerate your code!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_optimizer-1.0.0.tar.gz (143.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_optimizer-1.0.0-py3-none-any.whl (50.5 kB view details)

Uploaded Python 3

File details

Details for the file python_optimizer-1.0.0.tar.gz.

File metadata

  • Download URL: python_optimizer-1.0.0.tar.gz
  • Upload date:
  • Size: 143.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for python_optimizer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c25fc547eb17b05e13cfece76de011e3dab968ea8dab5c1ea5b5d5c1ac1f3af6
MD5 b3dae75776f14e51c67b359f4649f008
BLAKE2b-256 89a89979663401c86044d67adebf3c23f78287214efd26b411282a504d17a93e

See more details on using hashes here.

File details

Details for the file python_optimizer-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_optimizer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 96671243103e1e2f307e9831eb9cbdbbd46387ef199b2332eeae7ab81d307656
MD5 77a94d7ac980e0d0e0ac61aae8eef7d2
BLAKE2b-256 d0248642b7b48bd9707ad505ec1f347e74d05c291bfeeb3a6df8d75ac6e507f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page