TIP Project Report-MihirPatil
TIP Project Report-MihirPatil
optimized cpp
Submitted in partial fulfillment of the requirement for the award of
Degree of Bachelor of Technology in
Computer Science
Submitted To
SVKM’s NMIMS,
Mukesh Patel School of Technology Management & Engineering,
Shirpur Campus (M.H.)
Submitted by:
Mihir Patil - 70552100119
i
CERTIFICATE
This is to certify that the TIP project entitled Code Translation System has
been done by
____________ ___________
Project Mentor Examiner
(Internal Guide)
Date:
ii
TIP COMPLETION CERTIFICATE
iii
ACKNOWLEDGEMENT
I sincerely wish to express my grateful thanks to all members of the staff of computer
science department and all those who have embedded me with technical knowledge of
computer technology during various stages of B.Tech. Computer Science.
iv
ABSTRACT
v
TABLE OF CONTENTS
Sr.
Chapter No. Page
No.
1 INTRODUCTION
1.1 Background of project topic 1
1.2 Motivation and scope of report 2
1.3 Problem statement 3
1.4 Salient contribution 3
1.5 Organization of report 4
2 LITERATURE SURVEY
2.1 Introduction 5
2.2 Exhaustive Literature survey 5
References vi
vi
LIST OF FIGURES
vii
LIST OF TABLES
Sr. No. Table No. Name of Table Page
1 1 Gpt performance 19
2 2 Claude performance 20
3 3 Qwen-coder performance 20
viii
Chapter 1
Introduction
1
1.2 Motivation and scope of the report
The motivation for this research stems from the increasing need to combine the
productivity of Python with the efficiency of C++ in real-world applications.
Industries such as machine learning, gaming, and high-performance computing often
prototype in Python but require optimized implementations for deployment. Manual
translation is impractical at scale, and existing automated tools like Cython or Numba
offer limited flexibility, focusing on specific use cases (e.g., numerical computations)
rather than general-purpose translation. LLMs, with their generalization capabilities—
exemplified by GPT-4o’s versatility, Claude 3.5’s algorithmic insight, and Qwen2.5-
Coder32b’s coding specialization—present an opportunity to create a versatile, AI-
driven translation system that can handle diverse algorithms while optimizing for
speed.
The scope of this project is to develop an AI-powered framework that translates
Python code into optimized C++ using LLMs, evaluates the accuracy and
performance of the generated code, and benchmarks it against original Python
implementations. The study focuses on computational tasks—such as mathematical
approximations, array manipulations, and prime number generation—where
performance gains are most measurable. It excludes I/O-bound operations or complex
library dependencies to maintain a controlled evaluation environment. Potential
applications include accelerating legacy Python codebases, enhancing educational
tools, and supporting hybrid development workflows where prototyping and
production coexist, with Qwen2.5-Coder32b adding an open-source dimension to the
exploration.
4
Chapter 2
Literature survey
2.1 Introduction.
Automated code translation and optimization have been critical areas of research
since the development of early transpilers, which aimed to convert code between
high-level languages with minimal human intervention [4]. The emergence of Large
Language Models (LLMs) has revolutionized this domain by introducing AI-driven
approaches that leverage natural language processing and vast code repositories to
generate and optimize programs [5]. These models promise to transcend traditional
rule-based systems by understanding context, syntax, and semantics, making them
particularly suited for translating Python—a dynamically typed, interpreted
language—into C++, a statically typed, compiled language optimized for
performance. This section reviews foundational and recent works in Python-to-C++
translation, LLM-based code generation, and optimization techniques, establishing a
basis for identifying gaps addressed by this project.
The surveyed works reveal significant progress in AI-driven code generation and
optimization, yet several gaps persist. Early studies [1], [2] laid theoretical
foundations but did not address cross-language translation or performance
optimization. Tools like IntelliCode [3] and Codex [5] demonstrated LLM potential
but lacked focus on optimized C++ output. Optimization-focused works [4], [6] either
stayed within one language or relied on post-processing, missing LLM-driven
restructuring opportunities. Recent efforts [7], [8], [9], [10] show promise but either
lack empirical translation benchmarks, fail to maximize performance gains, or do not
compare multiple LLMs systematically. This project addresses these gaps by
developing an LLM-based system for Python-to-C++ translation, evaluating GPT-4o,
Claude 3.5, and Qwen2.5-Coder32b, and targeting significant execution speed
improvements through direct optimization.
Chapter 3
8
Methodology and Implementation
9
Figure 2: Use case Diagram
10
3) Activity diagram
11
4) class diagram
5) Sequence diagram
The Python to C++ translation project requires minimal hardware for basic operation
but benefits from more powerful systems when working with computationally
intensive algorithms:
13
Basic Requirements
14
Supported Algorithms
The system comes pre-loaded with four example algorithms of increasing complexity:
1. Hello World (basic output with timing)
2. Pi Calculation (numerical approximation using series)
3. Kadane's Algorithm (maximum subarray sum with custom random number
generation)
4. Sieve of Eratosthenes (prime number generation up to 100 million)
Technical Implementation
The software is built using Python with the following key components:
Gradio for the web interface
API integrations for multiple LLM providers
Cross-platform C++ compiler detection and execution
Streaming response handling for real-time code generation
Customizable system and user prompts for different translation tasks
Development Environment
The system requires:
Anaconda environment (Python 3.11)
C++ compiler (automatically detected)
API keys for selected LLM providers
HuggingFace endpoint for open source model integration
This software demonstrates practical applications of LLMs for code translation and
optimization, providing a valuable tool for developers looking to improve the
performance of Python algorithms through C++ implementations.
Flowchart
15
Figure 7: Flowchart
The system uses a modular approach where new models can be easily added by
implementing a streaming function and updating the model options list. The interface
allows users to:
1. Select from sample algorithms or input custom Python code
2. Choose which LLM to use for translation
3. Customize system and user prompts
4. Execute both Python and C++ versions
5. Compare execution times and outputs
16
User Interface
Figure 8: Prototype 1
17
Figure 10: Pi calculation
18
Chapter 4
Results and Analysis
1) Performance of gpt-4o
The table below presents the execution times for cpp-gpt and the corresponding
speedup compared to Python.
Algorithm Python (s) cpp-gpt (s) Speedup (x)
HelloWorld 0.00 0.00 1.00x
Pi Generator 14.13 0.22 64.23x
Kadane 49.28 0.89 55.37x
Sieve of Eratosthenes 39.89 0.72 55.40x
Table 1: gpt Performance
The cpp-gpt translations consistently outperform Python, with speedups ranging from
1.00x (HelloWorld, a negligible baseline) to 64.23x (Pi Generator). The Pi Generator
benefits significantly from C++’s floating-point optimizations, while Kadane and
Sieve of Eratosthenes show moderate but uniform improvements (around 55x),
suggesting cpp-gpt relies heavily on compiler optimizations like -O3 flags rather than
extensive algorithmic restructuring.
2) Performance of Claude
The table below presents the execution times for cpp-claude and the corresponding
speedup compared to Python.
19
Algorithm Python (s) cpp-claude (s) Speedup (x)
HelloWorld 0.00 0.00 1.00x
Pi Generator 14.13 0.24 58.88x
Kadane 49.28 0.001 49,280.00x
Sieve of Eratosthenes 39.89 0.69 57.81x
Table 2: Claude Performance
3) Performance of qwenCoder
The table below presents the execution times for cpp-qwenCoder and the
corresponding speedup compared to Python.
Algorithm Python (s) cpp-qwenCoder (s) Speedup (x)
HelloWorld 0.00 0.00 1.00x
Pi Generator 14.13 0.22 64.23x
Kadane 49.28 0.71 69.41x
Sieve of Eratosthenes 39.89 0.84 47.49x
Table 3: Qwen-coder Performance
20
Comparative Analysis
Across all models, HelloWorld serves as a baseline with no measurable speedup due
to its trivial execution time. For the Pi Generator, cpp-gpt and cpp-qwenCoder
achieve the highest speedup (64.23x), leveraging C++’s numerical efficiency, while
cpp-claude is slightly behind at 58.88x. The Kadane algorithm reveals the greatest
disparity, with cpp-claude’s 49,280.00x speedup dwarfing cpp-gpt’s 55.37x and cpp-
qwenCoder’s 69.41x, underscoring Claude 3.5’s superior algorithmic restructuring.
For the Sieve of Eratosthenes, all models perform comparably, with speedups
between 47.49x and 57.81x, indicating a memory-bound limit where further
optimization yields diminishing returns. Overall, cpp-claude excels in complex tasks,
cpp-gpt offers consistent performance, and cpp-qwenCoder provides a strong open-
source alternative, with performance varying by algorithm type.
21
Chapter 5
Advantages, Limitations and Applications
5.1 Advantages
The LLM-based Python-to-C++ translation system offers several key benefits, making
it a valuable tool for software development and performance optimization:
1. Significant Performance Gains: The system achieves dramatic speedups, ranging
from 47.49x to 49,280.00x across test cases, as demonstrated by the translations
from Python to C++. For instance, the Kadane algorithm’s execution time
dropped from 49.28 seconds in Python to 0.001 seconds with cpp-claude,
showcasing the potential for high-performance computing applications.
2. Automation and Efficiency: By automating the translation process, the system
eliminates the need for manual rewriting, saving developers substantial time and
effort. This is particularly beneficial for large codebases where manual
conversion would be impractical.
3. Model Versatility: The use of multiple LLMs (GPT-4o, Claude 3.5, and
Qwen2.5-Coder32b) allows the system to leverage each model’s strengths—
GPT-4o’s consistency, Claude 3.5’s algorithmic restructuring, and Qwen2.5-
Coder32b’s open-source accessibility—ensuring robust performance across
diverse algorithms.
4. Scalable Framework: The cross-platform execution pipeline, which includes code
generation, compilation, and benchmarking, is modular and reusable. This design
enables easy integration of new LLMs or adaptation to other language pairs,
enhancing the system’s longevity and applicability.
5. Enhanced Productivity: Developers can prototype in Python for its ease of use
and then seamlessly translate to C++ for production, bridging the gap between
rapid development and high-performance deployment without requiring deep
expertise in C++ optimization.
22
5.2 Limitations
Despite its strengths, the system has several limitations that warrant consideration:
1. Scope Limited to Computational Tasks: The current framework focuses on
computational algorithms (e.g., Pi Generator, Kadane) and excludes I/O-bound
operations or complex library dependencies. This restricts its applicability to
scenarios involving file handling, networking, or external APIs, which are
common in real-world applications.
2. Variability in LLM Outputs: The performance of translations varies across
models and algorithms. For example, while cpp-claude achieves a 49,280.00x
speedup for Kadane, its improvement for the Sieve of Eratosthenes (57.81x) is
less impressive compared to cpp-gpt (55.40x). This inconsistency highlights the
challenge of ensuring uniform optimization quality.
3. Prompt Sensitivity: The quality of the generated C++ code heavily depends on
the prompt design. Suboptimal prompts may lead to unoptimized or incorrect
translations, requiring expertise in prompt engineering to achieve the best results.
5.3 Applications
The system has a wide range of potential applications across various domains,
leveraging its ability to combine Python’s prototyping strengths with C++’s
performance:
1. High-Performance Computing (HPC): The system can accelerate scientific
simulations, such as those in physics or bioinformatics, where Python is often
used for prototyping but C++ is preferred for production due to speed
requirements. For example, the Pi Generator’s 64.23x speedup could
significantly reduce computation time in iterative mathematical models.
2. Machine Learning Deployment: Machine learning models are frequently
prototyped in Python using frameworks like TensorFlow or PyTorch. Translating
performance-critical components (e.g., inference loops) to C++ can optimize
deployment on resource-constrained devices, such as edge devices in IoT
applications.
3. Game Development: Game engines often require high performance for real-time
rendering and physics calculations. Developers can prototype game logic in
Python and use this system to translate critical sections to C++ for integration
23
into engines like Unreal or Unity, benefiting from speedups like those seen in
Kadane (up to 49,280.00x).
4. Educational Tools: The system can serve as a learning aid for students
transitioning from Python to C++, providing optimized translations as examples.
It can also help educators demonstrate the performance benefits of compiled
languages, using benchmarks like the Sieve of Eratosthenes (47.49x to 57.81x
speedup).
5. Legacy Code Optimization: Organizations with legacy Python codebases can use
this system to modernize their software, translating and optimizing code for
better performance without a complete rewrite. This is particularly useful in
industries like finance, where computational efficiency is critical for tasks like
risk analysis.
24
Chapter 6
Conclusion and Future Scope
6.1 Conclusion
This project has successfully developed and implemented a robust system for
translating Python code into high-performance C++ using a variety of Large
Language Models (LLMs), including GPT-4o, Claude 3.5, and Qwen2.5-Coder32b.
The system integrates a user-friendly Gradio-based interface that enables users to
select from four algorithmic samples—HelloWorld, Pi Generator, Kadane’s
Algorithm, and Sieve of Eratosthenes—ranging from simple output tasks to complex
computational challenges involving up to 100 million iterations. By leveraging
multiple LLM providers and HuggingFace endpoints, the system supports streaming
responses and allows for flexible model integration, enhancing its adaptability. The
execution environment, equipped with cross-platform C++ compiler detection and
automatic optimization flag selection, facilitates runtime comparisons that highlight
significant performance improvements.
The results demonstrate the system’s efficacy, with execution times for translated
C++ code outperforming Python across all test cases. Notably, the Kadane algorithm
achieved an extraordinary speedup of 49,280.00x with cpp-claude, while the Pi
Generator and Sieve of Eratosthenes saw consistent improvements of 58.88x to
64.23x across models. These findings underscore the potential of LLMs to not only
translate code accurately but also optimize it for performance, with Claude 3.5
excelling in algorithmic restructuring and Qwen2.5-Coder32b offering a competitive
open-source alternative. The modular design, allowing easy addition of new models
and customization of prompts, further enhances the system’s practicality. Overall, this
work bridges the gap between Python’s prototyping ease and C++’s execution
efficiency, providing a valuable tool for developers seeking automated performance
optimization.
25
6.2 Future Scope
The success of this project opens several avenues for future enhancement and broader
application. One key direction is the extension of the system to handle I/O-bound
tasks and complex library dependencies, which are currently excluded, thereby
expanding its utility to real-world scenarios involving file handling or network
operations. Incorporating advanced C++ features such as multi-threading, GPU
acceleration, and inline assembly could further amplify performance gains,
particularly for memory-intensive algorithms like the Sieve of Eratosthenes, where
parallelization could yield additional improvements.
Another promising area is the integration of additional LLMs, such as emerging
models from the open-source community or proprietary advancements, to diversify
the optimization strategies and potentially surpass the current best speedup of
49,280.00x. Enhancing the user interface with real-time performance visualization or
automated suggestion of optimal models based on algorithm type could improve
usability. Furthermore, developing a training pipeline to fine-tune LLMs specifically
for Python-to-C++ translation could reduce prompt sensitivity and ensure more
consistent optimization quality across diverse inputs.
26
References
1] S. Gulwani et al., "Program synthesis using natural language," IEEE Software, vol.
34, no. 5, pp. 12-19, 2017.
[2] M. Allamanis et al., "Learning to represent programs with graphs," ICLR, 2018.
[4] C. Cummins et al., "Compiler optimization with deep learning," ASPLOS, pp. 123-
134, 2021.
[5] M. Chen et al., "Evaluating large language models trained on code," arXiv
preprint arXiv:2107.03374, 2021.
[6] R. Li et al., "Code refactoring with LLMs," IEEE Trans. Softw. Eng., vol. 48, no.
3, pp. 456-467, 2022.
[8] Y. Wang et al., "Hybrid transpilation with LLMs," Proc. OOPSLA, pp. 89-102,
2023.
[9] T. Brown et al., "GPT-4: Advances in language modeling," OpenAI Tech Report,
2023.
[10] Anthropic Team, "Claude 3: A new frontier in reasoning," Anthropic Blog, 2024.
ix
Appendix A: Sample code
python_code_samples = {
python_sample_options[0]: """
import time
start_time = time.time()
print("Hello, world")
end_time = time.time()
python_sample_options[1]: """
import time
start_time = time.time()
result = calculate(100_000_000, 4, 1) * 4
end_time = time.time()
print(f"Result: {result:.12f}")
print(f"Execution Time: {(end_time - start_time):.6f} seconds")
""",
python_sample_options[2]: """
# Be careful to support large number sizes
x
def max_subarray_sum(n, seed, min_val, max_val):
lcg_gen = lcg(seed)
random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val
for _ in range(n)]
max_sum = float('-inf')
for i in range(n):
current_sum = 0
for j in range(i, n):
current_sum += random_numbers[j]
if current_sum > max_sum:
max_sum = current_sum
return max_sum
# Parameters
n = 10000
initial_seed = 42
min_val = -10
max_val = 10
python_sample_options[3]: """
import time
# Start timer
start_time = time.time()
# Stop timer
end_time = time.time()
# Print results
print("Maximum prime:", "{:,}".format(primes[-1]))
print("Execution Time: {:.6f} seconds".format(end_time - start_time))
"""
}
import platform
simple_cpp = """
#include <iostream>
int main() {
std::cout << "Hello";
return 0;
}
"""
def run_cmd(command_to_run):
try:
run_result = subprocess.run(command_to_run, check=True, text=True,
capture_output=True)
return run_result.stdout if run_result.stdout else "SUCCESS"
except:
return ""
def c_compiler_cmd(filename_base):
my_platform = platform.system()
my_compiler = []
try:
with open("simple.cpp", "w") as f:
f.write(simple_cpp)
xii
if my_platform == "Windows":
# Try MinGW g++ compiler
if os.path.isfile("./simple.exe"):
os.remove("./simple.exe")
# If no compiler found
if not my_compiler:
my_compiler = [my_platform, "Unavailable", []]
if not my_compiler:
if os.path.isfile("./simple"):
os.remove("./simple")
compile_cmd = ["clang++", "simple.cpp", "-o", "simple"]
if run_cmd(compile_cmd):
if run_cmd(["./simple"]) == "Hello":
xiii
my_compiler = ["Linux", "Clang++", ["clang++",
f"{filename_base}.cpp", "-o", f"{filename_base}"]]
if not my_compiler:
my_compiler=[my_platform, "Unavailable", []]
if not my_compiler:
my_compiler=[my_platform, "Unavailable", []]
except:
my_compiler=[my_platform, "Unavailable", []]
if my_compiler:
return my_compiler
else:
return ["Unknown", "Unavailable", []]
xiv