While Large Language Models (LLMs) have become remarkably adept at translating code between programming languages, they often suffer from a "literal translation" problem. Much like a novice human translator might translate an idiom word-for-word, LLMs tend to replicate the exact logic and structure of the source code. Consequently, they miss out on target-language optimizations, yielding code that functions correctly but runs significantly slower than human-written equivalents.
In the paper "Bridging Functional Correctness and Runtime Efficiency Gaps in LLM-Based Code Translation," researchers introduce SwiftTrans, a novel framework designed to ensure that translated code is not only functionally correct but also highly performant.
The Efficiency Deficit in Code Translation
Current LLM-based code translation systems face two major hurdles:
- The Efficiency Deficit: LLMs frequently replicate inefficient source constructs—such as C-style loops translated directly into Python—instead of utilizing idiomatic, high-performance built-in functions native to the target language.
- The Correctness-Efficiency Trade-off: Simply prompting a model to "write efficient code" often backfires. Models tend to take risky algorithmic shortcuts that introduce logical bugs, sacrificing correctness for speed.
Furthermore, existing benchmarks primarily evaluate whether the output is functionally correct, largely ignoring runtime performance. This lack of high-quality data and benchmarking tools has made it difficult to even measure the "efficiency gap."
The SwiftTrans Framework
To overcome these challenges, SwiftTrans operates in a two-stage pipeline that generates diverse candidates and intelligently selects the optimal translation.
Stage 1: Multi-Perspective Exploration (MpTranslator)
Instead of relying on repeated sampling from a single prompt—which often yields homogenous results or gibberish at high temperatures—this stage utilizes Parallel In-Context Learning (ICL). By providing the model with different sets of demonstration pairs, MpTranslator induces diverse translation strategies. These range from conservative, literal translations to highly optimized, idiomatic rewrites, ensuring a broad exploration of the solution space while remaining grounded in logic.
Stage 2: Difference-Aware Selection (DiffSelector)
To identify the best candidate, SwiftTrans employs a "pairwise judge" approach. Rather than asking an LLM to evaluate a single code snippet in isolation, DiffSelector evaluates the diff between two candidates. Highlighting modifications in a unified diff format makes subtle logic and efficiency differences much more apparent to the model. The system uses a linear-time selection strategy, inspired by bubble sort, to find the optimal candidate with minimal comparisons.
Specialized Training Strategies
To enable smaller open-source models (like Qwen2.5-7B) to execute these stages effectively, the researchers introduced two novel training techniques:
- Hierarchical Guidance: The translator is trained on a spectrum of code qualities, ranging from "functionally correct but slow" to "highly optimized."
- Ordinal Guidance: The selector is trained to rank code quality strictly (Efficient & Correct > Slow & Correct > Incorrect) and is made robust against the order in which candidates are presented.
Benchmarks and Datasets
To rigorously test runtime efficiency, the researchers utilized three primary benchmark categories:
- Extended CodeNet & F2SBench: Popular benchmarks were manually augmented with ten efficiency-critical test cases per sample, establishing strict maximum runtime constraints.
- SwiftBench (New): A novel benchmark featuring 500 source programs intentionally designed with inefficient patterns (e.g., redundant computations). This tests whether the translator can actively improve algorithmic efficiency during translation.
- Real-World Benchmarks: The framework was also evaluated on ClassEval-T, AlphaTrans, and RepoTrans to ensure generalizability to complex, repository-level code.
Key Results and Insights
By fine-tuning a 7B parameter model specifically for translation and selection, the researchers achieved results that surpass general-purpose models ten times their size.
- Superior Accuracy: On the new SwiftBench benchmark, SwiftTrans (powered by Qwen2.5-7B) achieved a 90.2% Computational Accuracy, outperforming GPT-5 (86.4%) and the previous state-of-the-art F2STrans (84.6%).
- Massive Speedups: SwiftTrans drastically reduced execution times. On SwiftBench, the average execution time for SwiftTrans was 292ms, compared to 528ms for GPT-5's correctness-only translations.
- Improved Maintainability: Beyond raw speed, the framework produced cleaner code with lower Cyclomatic Complexity (5.7 vs. GPT-5's 5.9), making the resulting programs easier to maintain.
Limitations and Future Directions
While SwiftTrans represents a significant leap forward, the authors note a few limitations. First, while it excels at function- and class-level translation, it was slightly outperformed by proprietary models on benchmarks featuring extremely long source programs (5,000+ tokens). Improving the long-context capabilities of underlying open-source models is a key next step.
Additionally, generating multiple candidates and running a pairwise selector introduces inference overhead. Although candidate generation is fully parallelizable, reducing this computational cost remains an ongoing goal. Finally, the authors emphasize that while efficiency is vastly improved, translated code must still undergo rigorous security testing, as aggressive optimization steps can occasionally introduce subtle vulnerabilities.
Summary updated