ECCO: Can We Improve Model-Generated Code Efficiency Without Sacrificing Functional Correctness?

Siddhant Waghjale, Vishruth Veerendranath, Zhiruo Wang, Daniel Fried

Abstract

Large Language Models (LLMs) have demonstrated significant proficiency in code generation, yet their ability to optimize code for efficiency remains a challenge. This paper introduces ECCO (Ensuring Correctness in Code Optimizations), a benchmark designed to evaluate the trade-off between code speed and functional correctness. Our findings reveal that while LLMs can improve runtime, they often introduce "spurious optimizations" that break logic. However, incorporating execution feedback and trajectory-based fine-tuning significantly improves performance.

Introduction: The Correctness-Efficiency Trade-off

While LLMs are increasingly capable of writing functional code, they often struggle to balance performance with correctness. When tasked with optimization, models frequently produce "spurious optimizations"—changes that reduce runtime but inadvertently break the program's logic.

Comparison of original code, a failed 'spurious optimization', and a successful 'correct optimization'.

Furthermore, measuring code efficiency is notoriously difficult due to hardware variability and environmental noise. The ECCO benchmark addresses these challenges by providing a reproducible framework and a standardized evaluation environment to reliably compare model performance.

Methodology and the ECCO Benchmark

The researchers curated the ECCO benchmark from the IBM CodeNet and AlphaCode datasets, comprising over 50,000 Python solution pairs across 1,380 competitive programming problems. The study investigated three primary optimization strategies:

  1. In-Context Learning (ICL): Utilizing instruction prompting and few-shot examples.
  2. Iterative Refinement: Comparing self-critique (Self-Refine) against raw execution feedback (Exec-Refine).
  3. Trajectory-Conditioned Fine-tuning: Training models on sequences of program versions (from slow to fast) to learn the human process of optimization.

To ensure hardware-agnostic and stable measurements, the framework employs JUDGE0, a sandboxed execution engine that provides precise runtime and memory metrics.

The Exec-Refine workflow using the Judge0 execution engine for feedback.

Key Results and Insights

The study highlights several critical findings regarding the current state of LLM-based code optimization:

  • Execution Feedback is Essential: Natural language feedback often fails to keep models grounded. In contrast, Exec-Refine significantly improves functional correctness. For example, on CodeLlama, Exec-Refine maintained a 54.6% correctness rate, whereas Self-Refine plummeted to 15.8%.
  • The Power of Trajectories: Trajectory-conditioned fine-tuning emerged as a top performer. By learning from intermediate human optimization steps, models like CodeLlama-7B saw correctness scores jump from ~43% to over 70%.
  • Model Personalities: "Instruct" models tend to be more aggressive in their optimization attempts, which frequently leads to broken code compared to their more cautious "Base" counterparts.
  • Scaling Limits: While larger models (e.g., CodeLlama-70B) generally offer better speedups, they do not always show linear improvements in correctness for editing tasks, suggesting that model size alone is not a complete solution.

Limitations and Future Directions

Currently, ECCO is focused on Python and competitive programming problems. Future work aims to expand the benchmark to other languages like C++ and Java, and to address real-world software engineering bottlenecks such as I/O and database query optimization. Additionally, preventing "multi-iteration decay"—where repeated refinement eventually breaks the code—remains a key challenge for the field.

Summary updated

BibTeX
@inproceedings{waghjale2024ecco,
  author = {Siddhant Waghjale and Vishruth Veerendranath and Zhiruo Wang and Daniel Fried},
  title = {{ECCO: Can We Improve Model-Generated Code Efficiency Without Sacrificing Functional Correctness?}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.859},
}
Figure