Skip to content

A high-performance demonstration project showing how to dramatically accelerate Python functions using C++ extensions. This project implements the same computational algorithms in both pure Python and optimized C++, then compares their performance to showcase the incredible speedups with C++.

License

Notifications You must be signed in to change notification settings

furkancanisci/cpythonwrapper

Repository files navigation

πŸš€ Python + C++ Performance Wrapper

License: MIT Python C++ pybind11

A high-performance demonstration project showing how to dramatically accelerate Python functions using C++ extensions. This project implements the same computational algorithms in both pure Python and optimized C++, then compares their performance to showcase the incredible speedups possible with C++ acceleration.

🎯 Performance Results

Function Python Time C++ Time Speedup Improvement
Sum of Squares (n=100K) 5.765ms 0.003ms 1,977x 197,600%
Fibonacci (n=35) 1.708s 0.030s 57x 5,600%
Fibonacci Memoized (n=40) 0.323s 0.000009s 96,661x 9,666,000%
Prime Count (10K) 5.158ms 0.065ms 79x 7,800%
Matrix Mult (100x100) 85.720ms 0.446ms 192x 19,100%

πŸ’‘ Key Insight: C++ can provide speedups ranging from 50x to over 96,000x for computational tasks!

✨ Features

  • πŸ“Š Performance Comparison: Side-by-side analysis of Python vs C++ implementations
  • πŸ”— Seamless Integration: Call C++ functions from Python using pybind11
  • ⚑ Optimized Algorithms: Leverage C++'s power with advanced algorithms and mathematical formulas
  • πŸ“Š Comprehensive Benchmarks: Detailed performance measurements and analysis
  • πŸš€ Easy Setup: Simple installation and build process with Makefile support
  • πŸ“š Interactive Demos: Test individual functions and see real-time performance differences
  • πŸ”§ Production Ready: Clean, documented code suitable for real-world applications

πŸ“ Project Structure

cpythonwrapper/
β”œβ”€β”€ python_implementation.py    # Pure Python implementations
β”œβ”€β”€ cpp_functions.h            # C++ function headers
β”œβ”€β”€ cpp_functions.cpp          # C++ implementations
β”œβ”€β”€ pybind_wrapper.cpp         # Python-C++ bridge (pybind11)
β”œβ”€β”€ setup.py                   # Build configuration
β”œβ”€β”€ performance_benchmark.py   # Performance comparison script
β”œβ”€β”€ demo.py                    # Interactive demonstration
β”œβ”€β”€ Makefile                   # Easy build management
β”œβ”€β”€ requirements.txt           # Python dependencies
└── README.md                  # This file

πŸš€ Quick Start

Option 1: Using Makefile (Recommended)

# Clone the repository
git clone https://github.com/yourusername/cpythonwrapper.git
cd cpythonwrapper

# Setup everything
make setup

# Build C++ extension
make build

# Run tests
make test

# Run full benchmark
make benchmark

# Interactive demo
make demo

Option 2: Manual Installation

Requirements

  • Python 3.6 or higher
  • C++ compiler (GCC, Clang, or MSVC)
  • pip package manager

Step 1: Install Dependencies

pip install -r requirements.txt

Step 2: Build C++ Extension

python setup.py build_ext --inplace

Step 3: Test

python performance_benchmark.py --quick

πŸƒβ€β™‚οΈ Usage

Quick Test

python performance_benchmark.py --quick

Full Performance Analysis

python performance_benchmark.py

Interactive Demo

python demo.py

Using from Python

# Pure Python implementations
import python_implementation

result = python_implementation.sum_of_squares(100000)
print(f"Python result: {result}")

# C++ accelerated functions
import cpp_accelerated

result = cpp_accelerated.sum_of_squares(100000)
print(f"C++ result: {result}")

# Optimized versions
result = cpp_accelerated.sum_of_squares_optimized(100000)
print(f"Optimized C++ result: {result}")

πŸ“Š Benchmark Functions

1. Sum of Squares

Calculates the sum of squares from 1 to n.

Python:

def sum_of_squares(n):
    total = 0
    for i in range(1, n + 1):
        total += i * i
    return total

C++:

long long sum_of_squares(int n) {
    long long total = 0;
    for (int i = 1; i <= n; ++i) {
        total += static_cast<long long>(i) * i;
    }
    return total;
}

C++ Optimized (Mathematical Formula):

long long sum_of_squares_optimized(int n) {
    // Using formula: n(n+1)(2n+1)/6
    long long ln = static_cast<long long>(n);
    return (ln * (ln + 1) * (2 * ln + 1)) / 6;
}

2. Fibonacci Sequence

Calculates Fibonacci numbers using recursive and memoized approaches.

3. Prime Counting

Counts prime numbers up to a given limit using trial division and Sieve of Eratosthenes.

4. Matrix Multiplication

Performs matrix multiplication with cache-friendly access patterns.

🎯 Real Performance Results

Based on actual benchmarks run on Apple Silicon:

Sum of Squares (n=100,000)

  • Python: 5.765ms
  • C++: 0.003ms
  • Speedup: 1,977x faster

Fibonacci (n=35)

  • Python: 1.708s
  • C++ Recursive: 0.030s
  • C++ Memoized: 0.000009s
  • Speedup: 96,661x faster with memoization

Prime Count (limit=10,000)

  • Python: 5.158ms
  • C++ Trial Division: 0.291ms
  • C++ Sieve Algorithm: 0.065ms
  • Speedup: 79x faster with optimized algorithm

Matrix Multiplication (100x100)

  • Python: 85.720ms
  • C++: 0.446ms
  • Speedup: 192x faster

πŸ”§ Advanced Usage

Custom Benchmarks

import cpp_accelerated
import time

def custom_benchmark():
    start = time.perf_counter()
    result = cpp_accelerated.sum_of_squares(1000000)
    end = time.perf_counter()
    print(f"Result: {result}, Time: {end-start:.6f}s")

custom_benchmark()

Algorithm Optimizations

The C++ version includes several optimization techniques:

import cpp_accelerated

# Standard implementation
result1 = cpp_accelerated.sum_of_squares(100000)

# Mathematical formula optimization
result2 = cpp_accelerated.sum_of_squares_optimized(100000)

# Sieve of Eratosthenes for prime counting
primes = cpp_accelerated.prime_count_optimized(100000)

# Memoization for Fibonacci
fib = cpp_accelerated.fibonacci_memoized(50)

πŸ“– Available Commands

The project includes a comprehensive Makefile:

make setup       # Create venv and install dependencies
make build       # Build the C++ extension
make test        # Run quick functionality test
make benchmark   # Run full performance comparison
make demo        # Run interactive demo
make check       # Check if C++ extension is working
make clean       # Clean build artifacts
make clean-all   # Clean everything including venv
make rebuild     # Clean and rebuild
make help        # Show all available commands

πŸ› Troubleshooting

Build Errors

Problem: error: Microsoft Visual C++ 14.0 is required Solution: Install Visual Studio Build Tools on Windows

Problem: fatal error: 'pybind11/pybind11.h' file not found Solution: Reinstall pybind11: pip install --upgrade pybind11

Problem: C++ compiler not found Solution:

  • Windows: Visual Studio Build Tools
  • macOS: Xcode Command Line Tools (xcode-select --install)
  • Linux: GCC (sudo apt install build-essential)

Import Errors

Problem: ImportError: No module named 'cpp_accelerated' Solution: Build the extension: python setup.py build_ext --inplace

Problem: Architecture mismatch on macOS Solution: The setup.py includes architecture flags to handle this automatically.

πŸ“ˆ Performance Tips

  1. Large Data Sets: C++ advantages become more pronounced with larger datasets
  2. Algorithm Choice: Sometimes algorithmic improvements matter more than language choice
  3. Memory Management: C++ provides better memory control and cache efficiency
  4. Parallelization: C++ offers superior tools for parallel computing

πŸŽ“ Educational Value

This project demonstrates several important concepts:

  • Python C Extensions: How to extend Python with C++
  • Performance Optimization: Real-world performance comparison
  • Algorithm Design: Different approaches to the same problem
  • Build Systems: Modern C++ build integration with Python
  • Benchmarking: Proper performance measurement techniques

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

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

πŸ”— Resources

πŸ“ž Contact

If you have any questions or suggestions, please open an issue or submit a pull request.


Note: This project is designed for educational purposes to demonstrate the performance differences between Python and C++. For production use, please adapt and optimize according to your specific requirements.

About

A high-performance demonstration project showing how to dramatically accelerate Python functions using C++ extensions. This project implements the same computational algorithms in both pure Python and optimized C++, then compares their performance to showcase the incredible speedups with C++.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published