Source-linked AI summary
PerfCodeGen: Improving Performance of LLM Generated Code with Execution Feedback
Yun Peng, Akhilesh Deepak Gotmare, Michael Lyu, Caiming Xiong, Silvio Savarese, Doyen Sahoo
TL;DR
Code-generation evaluations have largely focused on functional correctness, despite the importance of runtime efficiency. PERFCODEGEN incorporates execution feedback into iterative self-refinement, and its evaluation reports improved correctness and runtime efficiency across programming benchmarks, including state-of-the-art runtime efficiency on portions of HumanEval and MBPP.
Problem
Code-generation evaluations have largely emphasized functional correctness, leaving runtime efficiency comparatively underexamined despite its importance to software design.
Source: §1 · p. 1Method
PERFCODEGEN uses unit-test execution feedback to refine code for correctness, then uses feedback about the most expensive test to guide runtime-efficiency refinement.
Source: §1 · p. 2Results
Across HumanEval, MBPP, and APPS, PERFCODEGEN shows gains in correctness and runtime efficiency; Phi-3-mini reaches %Opt 40.85 @ k = 8, comparable to GPT-4 base %Opt 39.26 (k = 8).
Source: §3.2 · p. 6, §5 · p. 9Takeaways & Limitations
PERFCODEGEN achieves state-of-the-art runtime efficiency on a sizeable fraction of HumanEval and MBPP problems, with GPT-4 frequently exceeding ground-truth reference solutions.
Source: §5 · p. 9The study assumes access to all task unit tests for optimization, so its correctness metric is not directly comparable with work that reserves tests for evaluation.
Source: §3.1 · p. 5
Abstract
from arXiv · showhide
Large Language Models (LLMs) are widely adopted for assisting in software development tasks, yet their performance evaluations have narrowly focused on the functional correctness of generated code. Human programmers, however, require LLM-generated code to be not only correct but also optimally efficient. We propose PerfCodeGen, a training-free framework that enhances the performance of LLM-generated code by incorporating feedback based on runtime during test case execution into the self-refinement iterations. With PerfCodeGen, we achieve speedups for a significantly higher proportion of problems compared to using the base LLM with sophisticated prompting techniques. Applied to open language models like Phi-3-mini, PerfCodeGen achieves runtime efficiency comparable to prompting powerful closed models like GPT-4. We achieve state-of-the-art runtime efficiency on benchmarks such as HumanEval, MBPP, and APPS, frequently surpassing the ground truth reference solutions with PerfCodeGen using GPT-3.5 and GPT-4. Additionally, we demonstrate the effectiveness of our approach in enhancing code quality across a range of open LLMs of varying sizes including Phi-3-mini, Llama 3 8B, Mixtral 8x7B, Command R, and Llama 3 70B.
1 Introduction
Code-LM evaluation has largely emphasized functional correctness, although runtime efficiency affects software quality and practical costs. PERFCODEGEN uses execution feedback during self-refinement to improve generated code beyond correctness alone.
1 Introduction
- Code-LM evaluations have focused almost exclusively on functional correctness, often overlooking runtime efficiency and other code-quality aspects.Runtime efficiency affects user experience, serving costs, and carbon footprint.Source: §1 · p. 1
- Prior performance-focused fine-tuning methods require parallel training data and a functionally correct input, and do not use unit-test execution feedback.Prompting and tool-feedback approaches also often fail to assess or improve code quality beyond functionality.Source: §1 · p. 2
- PERFCODEGEN first uses unit-test feedback to refine correctness, then targets the most expensive test with runtime feedback for performance refinement.It is evaluated on HumanEval, MBPP, and APPS using open and closed models of varying sizes.Source: §1 · p. 2
2 PERFCODEGEN Methodology
PERFCODEGEN samples candidate programs, uses unit-test feedback to refine incorrect code, and then optimizes correct solutions using execution-time feedback. The performance phase targets the slowest test and retains a correct fallback if refinements fail.
2 PERFCODEGEN Methodology
- PERFCODEGEN samples K candidate programs and uses an execution environment to collect correctness feedback before its correctness and performance phases.The framework is illustrated in Figure 1.Source: §2 · p. 3
2.1 Generating Correct Code
- Incorrect candidates are iteratively refined using a failing unit test and verbalized execution feedback, then checked again for correctness.The method performs one correctness-refinement iteration to limit computational costs, as gains diminish after the first iteration.Source: §2.1 · p. 3
2.2 Optimising Correct Code using Execution Feedback
- For each correct candidate, the method measures execution time across unit tests, identifies the slowest test, and feeds it back to guide performance refinement.Execution-time estimates exclude the smallest and largest of E observations to reduce measurement noise; if a refinement fails correctness, the fastest correct candidate is returned.Source: §2.2 · pp. 3–4
- The final solution is the fastest correct refined output, or the fastest correct base candidate when no refinement passes.Performance refinement uses one greedy-decoded refinement per correct candidate to reduce inference costs.Source: §2.2 · p. 4
3 Experiments
The experiments evaluate correctness and runtime efficiency across three benchmarks, comparing PERFCODEGEN with base LLMs and alternative prompting strategies. Results show gains in optimization on HumanEval and MBPP, while APPS outcomes vary by model and task difficulty.
3.1 Setup: Metrics, Datasets and Models
- The experiments compare the fastest correct program among k samples using %Opt, %Correct, and speedup, estimating execution time with 12 executions per unit test.%Opt counts problems where a generated program is at least 10% faster than the ground truth; APPS uses its fastest ground-truth solution as reference.Source: §3.1 · pp. 4–5
3.3 Evaluating Alternatives to PERFCODEGEN’s Execution Feedback
- With GPT-3.5 at k = 1, PERFCODEGEN gains 6.17% in %Opt on HumanEval and 3.06% on MBPP, outperforming the alternative prompting approaches.Other performance-improvement prompting, multi-round, and multi-agent approaches produce insignificant or often negative gains; direct execution feedback yields a 5.55% gain on HumanEval and a 1.31% drop on MBPP.Source: §3.3 · pp. 7–8, Table 3 · p. 8
3.4 Role of Planning in Correctness Refinement
- Across HumanEval and MBPP with GPT-3.5 and Llama 3 70B, planning generally yields higher correctness than the direct approach at k = 8 and 20, and PERFCODEGEN achieves the highest correctness rates on both benchmarks.At k = 1, PERFCODEGEN’s additional planning step often leads to slightly lower correctness gain than the direct approach.Source: §3.4 · p. 9
4 Related Work
Prior work improves code quality through code-to-code edits and LLM self-refinement, while PERFCODEGEN focuses on runtime efficiency in generated code. The section also contrasts that training-free refinement direction with preference-based approaches that require data and fine-tuning.
4 Related Work
- Earlier code-to-code editing work targets bug fixing, performance, maintainability, and security, whereas this work frames runtime improvement as text-to-code prompting.The authors argue that code-quality research should study natural-language inputs expressing developer intent or program specifications.Source: §4 · p. 9
- Preference-based methods such as RLHF and DPO rely on human preference data, while RL also entails costly fine-tuning and training instability.The passage notes that large-scale code preference data may be more difficult and expensive to collect than natural-language response preferences.Source: §4 · p. 9
- PERFCODEGEN extends LLM self-refinement and tool-feedback approaches from functional correctness to runtime efficiency in code generation.Prior approaches add planning or analysis stages, or use environment feedback; Reflexion focuses on functional correctness.Source: §4 · p. 9
5 Conclusion
PERFCODEGEN uses code execution feedback during iterative LLM self-refinement to improve generated-code runtime efficiency. The authors report gains across benchmarks and models, including state-of-the-art runtime efficiency on a sizeable fraction of HumanEval and MBPP problems.
5 Conclusion
- PERFCODEGEN achieves state-of-the-art runtime efficiency on a sizeable fraction of HumanEval and MBPP problems, sometimes far exceeding ground-truth solutions with GPT-4.The authors also report consistent and significant gains in correctness and runtime efficiency across three Python benchmarks and open and closed models of varying sizes.Source: §5 · p. 9
- PERFCODEGEN incorporates code execution feedback into iterative LLM self-refinement to improve the runtime efficiency of generated code.The authors report that Phi-3-mini can achieve code quality comparable to GPT-4 with this approach.Source: §5 · p. 9
- The authors identify integrating execution feedback into code generation as a path toward more robust and reliable AI-driven software development.Source: §5 · p. 10
A Limitations
The limitations concern the scale and objectives of optimization, as well as the cost and variability of runtime measurement. PERFCODEGEN currently targets small Python components and runtime performance rather than broader project architecture or other code qualities.
A Limitations
- The evaluation focuses on performant modules or Python functions, not architectural patterns across entire project repositories.The authors identify minimal redundancy and wasteful computation across a project as broader concerns outside the current scope.Source: A Limitations · p. 14
- PERFCODEGEN measures runtime performance but not memory consumption, readability, maintainability, security, or harmlessness.The authors say balancing optimization across these dimensions is non-trivial.Source: A Limitations · p. 14
- Runtime measurements can vary across compute environments, and averaging 10 independent runs adds execution cost.The study controls environments and runs one program at a time; future work could explore deterministic instruction counts.Source: A Limitations · p. 14
B.1 Sanitized Benchmarks
The evaluation uses sanitized HumanEval, MBPP, and APPS benchmarks for correctness, and common subsets for time-efficiency assessment. The benchmark table describes problem, ground-truth-program, and testcase counts.
B.1 Sanitized Benchmarks
- Table 5 reports each sanitized benchmark’s problem count and average ground-truth-program and testcase counts per problem.The table distinguishes benchmarks used for correctness evaluation from common subsets used for time-efficiency evaluation.Source: Table 5 · p. 14
- The evaluation retains 3,249 instances for correctness after excluding APPS instances whose ground-truth solutions fail the exact-match tests.The authors identify 1,511 APPS instances with ground-truth failures, attributing the discrepancy to exact-match evaluation versus APPS similar-match criteria and possible incorrect references.Source: §B.1 · p. 14
- Time-efficiency data come only from benchmark subsets where every baseline method generates at least one functionally correct program.Source: §B.1 · p. 15
B.2 Compute
The experiments used substantial compute for both LLM inference and execution of generated programs; execution was the costlier bottleneck.
- Inference ran on 16 Nvidia A100 GPUs with vLLM for approximately three weeks.The experiments used gpt-4-0613 and gpt-3.5-turbo-0125 endpoints from OpenAI.Source: §B.2 · p. 15
- The OpenAI API experiments required nearly 411k GPT-4 and 1.5M GPT-3 requests, contributing to the study’s major costs.Source: §B.2 · p. 15
- Executing generated programs used 40 16-CPU, 104-GB-RAM GCP instances for roughly four weeks and cost more than LLM inference.The setup also implemented safeguards prescribed in Section 2.3 of Chen et al. [2021] to mitigate risks from untrusted programs.Source: §B.2 · p. 15
B.3 Statistical Significance
The authors report significant gains in %Opt over the base LLM, with statistical significance varying by benchmark.
- PERFCODEGEN yields significant gains in %Opt compared with the base LLM.Source: §B.3 · p. 15
- The improvement is statistically significant at α < 0.05 on MBPP and APPS, but reaches only α < 0.2 on HumanEval.The authors attribute the lower confidence on HumanEval to its smaller problem count of 164.Source: §B.3 · p. 15
- For GPT-4, Z-scores at k = 1 and k = 8 are −0.878 and −1.34 on HumanEval, −2.588 and −2.947 on MBPP, and −1.8 and −2.675 on APPS.Source: §B.3 · p. 15
B.4 Broader Impact
The authors describe potential benefits of more efficient generated code alongside risks involving misuse, security, maintainability, and employment.
- The authors suggest more efficient code could accelerate software development, reduce development costs and execution energy, and make open LLMs more competitive with closed models.They also propose that it could widen access to basic programming and offer a route to discovering more efficient algorithms for longstanding problems (Romera-Paredes et al. [2024]).Source: §B.4 · p. 15
- Potential risks include malicious-code misuse, vulnerabilities in optimized code, and debugging or maintenance challenges if generated code is less readable.The authors recommend filtering, malicious-code detection, testing, validation, and code reviews to help reduce these risks.Source: §B.4 · p. 15
- The authors note that further automation in the software industry could displace some programmers and call for discussion of re-skilling initiatives.Source: §B.4 · p. 15
- Table 6 reports PERFCODEGEN results on HumanEval, MBPP, and APPS with k = 20.Source: Table 6 · p. 16
B.6 Examples of Optimal Solutions Generated by LLMs
The examples include a PERFCODEGEN-generated FibFib solution, while the prompts show correctness feedback followed by runtime-optimization requests and multi-agent variants.
B.6 Examples of Optimal Solutions Generated by LLMs
- The FibFib example asks for an efficient n-th sequence element and labels the displayed PERFCODEGEN solution as optimal.Source: §B.6 · p. 17
- The generated FibFib code updates three running values in a loop, whereas the cited ground-truth solution uses recursive calls.Source: §B.6 · p. 17
B.7 Prompts
- The reflection-and-test-case-feedback prompt presents a failed test case and error, asks for a failure analysis and strategy, then requests a corrected solution.Source: §B.7 · p. 19, Figure 3 · p. 19
- The runtime-optimization prompts ask for a faster solution while preserving functional equivalence and the input-output format; one variant first asks for an optimization strategy.Source: §B.7 · pp. 20–23, Figure 4 · p. 20, Figure 5 · p. 21
- The prompt appendix also includes a Coder–Reviewer variant and a Leader–Coder–Reviewer team prompt.Source: Figure 8 · p. 24, Figure 9 · p. 25, §B.7 · pp. 25–26