An LLM-powered developer agent that translates Python code into standard, optimized C++17, compiles it with g++ -O3, runs both versions side-by-side, and generates a detailed performance and correctness benchmark report.
- Robust Code Extraction: Extracts raw Python scripts even when wrapped inside markdown code blocks.
- LLM-Powered Translation: Translates complete logic structures (variables, arrays, functions, loops) to idiomatic, standard C++17.
- Auto-Compilation: Uses
g++ -O3 -std=c++17to compile translated source files into static executables. - Performance Evaluation: Runs both implementations concurrently with customized timeout caps and measures runtime using high-precision timers.
- Correctness Normalization: Normalizes terminal outputs (collapsing spaces, ignoring execution duration outputs) to verify algorithmic correctness without false alarms.
- Interactive Workbench: Comes with both a rich command-line tool (
cli.py) and an elegant Gradio browser UI (app.py).
Python2CPP-Agent/
βββ README.md # Detailed introduction and user manual
βββ LICENSE # MIT License
βββ CONTRIBUTING.md # Open-source contributions guidelines
βββ CODE_OF_CONDUCT.md # Community behavioral expectations
βββ requirements.txt # PIP dependencies manifest
βββ pyproject.toml # Packaging metadata and CLI commands
βββ .gitignore # Ignored cache/output files config
βββ app.py # Gradio Web Workbench app
βββ cli.py # CLI command line application
βββ agent/ # Core Modular Package
β βββ __init__.py # Package initializer
β βββ translator.py # OpenAI-compatible translator client
β βββ compiler.py # Subprocess compiler manager
β βββ benchmark.py # Normalizer and speedup metric computer
β βββ executor.py # Safe, timed python/C++ execution wrapper
β βββ report.py # Markdown/JSON formatting engine
β βββ utils.py # Text parsing and file helpers
β βββ prompts.py # Translation prompts and LLM constraints
βββ examples/ # Demonstration Scripts
β βββ fibonacci.py # Iterative Fibonacci checks
β βββ matrix.py # Dense 3D matrix multiplication
β βββ sorting.py # Vector Bubble Sort swaps
β βββ prime.py # Prime number check loops
βββ tests/ # Lightweight Unit Tests
β βββ test_extract.py # Code extraction parser test
β βββ test_compare.py # Whitespace and timing filters test
β βββ test_executor.py # Process execution constraints test
β βββ test_report.py # Markdown formatting validation test
βββ docs/ # Architectural Guides
β βββ architecture.md # Pipeline design overview
β βββ benchmark.md # Correctness and performance details
β βββ api.md # Library API specifications
βββ benchmark/ # Past Run Logs
βββ results.json # Performance history database
βββ report.md # Pre-compiled results summary
- Python: 3.10 or newer.
- C++ Compiler: A C++17 compatible
g++compiler on your system path.- Linux (Ubuntu/Debian):
sudo apt install g++
- macOS: Install Xcode Command Line Tools by running:
xcode-select --install
- Windows: Install MinGW-w64 (e.g., via MSYS2 or WinLibs) or Visual Studio Build Tools.
- Linux (Ubuntu/Debian):
Clone this repository and install dependencies:
git clone https://github.com/BenyaminMahdavifar/Python2CPP-Agent.git
cd Python2CPP-Agent
pip install -r requirements.txtSet up one of the following environment variables depending on your chosen API provider:
Using Google Gemini API (Recommended & Free options):
export GEMINI_API_KEY="your-gemini-key"Using OpenRouter:
export OR_API_KEY="your-openrouter-key"Using OpenAI:
export OPENAI_API_KEY="your-openai-key"Run translation and performance evaluations directly from your terminal:
# Run on an existing file
python cli.py -f examples/fibonacci.py
# Run on a raw code string
python cli.py -c "print(sum(i for i in range(100000)))"
# Change output directory and save as JSON
python cli.py -f examples/prime.py -o custom_workspace/ --jsonLaunch a local visual workspace directly in your web browser:
python app.pyOpen http://localhost:3000 in your browser to start.
We use pytest for unit testing. Install the dev dependencies and run tests:
pip install -e .[dev]
pytestBenyamin Mahdavifar
- GitHub Profile: @BenyaminMahdavifar
- Email: benjamincrayan@gmail.com
- Repository: Python2CPP-Agent
- Bug Tracker: Issues
This project is licensed under the terms of the MIT License. See LICENSE for details.