Abstract
Modern Code Language Models (CLMs) excel at generating functional code but often remain "performance-blind," producing verbose or computationally inefficient solutions. Previous attempts to optimize for execution speed frequently suffered from a significant "correctness tax." We introduce Code-Optimise, a lightweight framework that enables CLMs to improve both accuracy and efficiency by training on self-generated preference data. Using Direct Preference Optimization (DPO) to distinguish between solutions based on execution speed and functional success, our approach reduces code runtime by up to 6% and code length by up to 48% while simultaneously boosting functional correctness.
Introduction
While state-of-the-art CLMs are proficient at solving programming tasks, their training objectives typically focus on token prediction or functional correctness, ignoring execution efficiency. This leads to models that are indifferent to runtime performance or code verbosity. Existing optimization strategies often rely on distillation from larger, proprietary models or result in drastic drops in accuracy. Code-Optimise addresses these issues by using a model's own outputs to create a preference-based training signal, ensuring efficiency gains do not come at the cost of correctness.
The Code-Optimise Framework
The framework operates through a three-stage pipeline: Sampling, Annotation, and Optimization.
- Sampling: A baseline model (e.g., CodeLlama or StarCoder) generates 100 diverse solutions for a set of seed programming problems using multinomial sampling.
- Annotation: An automated interpreter runs these solutions against unit tests. A specialized Timing Module executes each solution 50 times to ensure stable runtime measurements. Solutions are then labeled as Passed/Failed and Quick/Slow.
- Optimization: The model is fine-tuned using Direct Preference Optimization (DPO). Unlike standard Supervised Fine-Tuning (SFT), which only learns from positive examples, DPO utilizes pairs of solutions (e.g., a fast correct solution vs. a slow correct solution) to teach the model explicit preferences.
A critical innovation is Dynamic Solution Selection (DSS), which randomly samples new preference pairs from the generated pool at the start of every epoch. This prevents overfitting to specific examples and encourages the model to develop a more robust understanding of what makes code "good."
Results and Analysis
Experiments across StarCoder and CodeLlama families demonstrate that Code-Optimise consistently improves both correctness and efficiency. While standard SFT often reinforces verbosity—making code longer and slower—DPO acts as an "efficiency engine" by explicitly penalizing undesirable traits.
Key findings include:
- Efficiency Gains: Runtimes were reduced by up to 6% on in-domain tasks (MBPP) and 3% on out-of-domain tasks (HumanEval) compared to competitive baselines.
- Code Conciseness: The model learned that brevity often correlates with efficiency, leading to a reduction in average code length by up to 48%.
- Correctness Improvements: Functional correctness (pass@k) improved significantly; for instance, StarCoder-1B's
pass@1on MBPP jumped from 11.8% to 17.1%. - Scalability: The framework's benefits scaled effectively with model size, with 13B parameter models showing the strongest performance in both correctness and efficiency.
Conclusion
Code-Optimise provides a self-sufficient methodology for developing CLMs that are both accurate and computationally lean. By moving beyond simple correctness to include execution performance in the optimization loop, we can produce models that generate higher-quality, production-ready code. Future work will explore extending this framework to low-level languages like C++ and improving runtime measurement precision for micro-benchmarks.
Summary updated