NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Large Language Models as Code Executors: An Exploratory Study

Chenyang Lyu, Lecheng Yan, Rui Xing, Wenxi Li, Younes Samih, Tianbo Ji, Longyue WangarXiv 2024

Abstract

While Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation, their ability to internally execute code—predicting exact outputs without external interpreters—remains less understood. This study evaluates state-of-the-art models on their capacity for "mental" code execution. We find that while most models struggle, OpenAI’s o1-preview achieves over 90% accuracy. Furthermore, we introduce Iterative Instruction Prompting (IIP), a novel technique that significantly enhances performance by mimicking step-by-step human debugging.

Introduction

The ability of LLMs to write code is well-documented, but whether they truly "understand" the underlying logic or merely perform sophisticated pattern matching is a subject of debate. This research posits that code execution serves as a superior proxy for evaluating an LLM's grasp of logic, state tracking, and causality. If a model cannot accurately predict a program's output, its understanding of the code's semantics is likely superficial. This capability is critical for developing advanced automated debugging tools and intelligent programming assistants. Despite its importance, the field has lacked a comprehensive benchmark comparing how different state-of-the-art models handle direct execution.

Methodology

The study evaluates several prominent models, including OpenAI’s o1-preview, GPT-4o, GPT-3.5, DeepSeek-Coder, and Qwen-2.5-Coder. To assess their execution capabilities, three prompting strategies were compared:

  1. Vanilla Prompting (VP): A direct request for the code's output without additional guidance.
  2. Chain-of-Thought (CoT): Encouraging the model to "think step-by-step" before outputting the final result, leveraging the model's internal reasoning capabilities.
  3. Iterative Instruction Prompting (IIP): A novel approach introduced by the authors where code is fed to the model line-by-line or in small segments. This forces the model to explicitly update its internal variable state at each step, simulating a human developer tracing execution during a debugging session.
Overview of the three prompting methods evaluated: Vanilla, Chain-of-Thought, and the proposed Iterative Instruction Prompting.

Experimental Setup

The researchers utilized a curated dataset of 200 code snippets sourced from LeetCode, split equally between English and Chinese examples. These snippets span various algorithmic domains such as Dynamic Programming (DP), Greedy algorithms, String manipulation, Math, and Sorting. Each entry includes problem descriptions, test cases, standard solutions, and human "pass rates" to provide a baseline for difficulty.

Key Insights and Results

The "o1" Paradigm Shift

A significant finding is the substantial performance gap between OpenAI’s o1-preview and other models. While GPT-4o and specialized coding models like DeepSeek-Coder typically achieved 50-70% accuracy, o1-preview consistently exceeded 90%. Specifically, it reached 93.5% accuracy on Chinese data and 96.1% on English data. This suggests that o1's internal reasoning tokens are uniquely suited for maintaining state during complex logical tasks.

Conceptual diagram of OpenAI-o1 executing a code snippet through internal reasoning and state tracking.

Effectiveness of Iterative Instruction Prompting

The IIP method demonstrated that granular state-tracking is more effective than monolithic reasoning. On average, IIP boosted the accuracy of weaker models by 7.22%, with some models like Qwen-Coder seeing improvements as high as 18.96%. IIP outperformed standard CoT by an absolute average of 3.86%, proving that forcing models to rethink the output after every line is a superior strategy for execution tasks.

Algorithmic Complexity and State Degradation

Models performed best on deterministic search algorithms (e.g., Binary Search, ~63% avg accuracy) but struggled with Bit Manipulation and Dynamic Programming (~41-43% accuracy), which involve recursive logic and complex state transitions. Additionally, a clear negative correlation was observed between code length and accuracy; as the "lines of code" count increases, models are more prone to "hallucinating" variable states. Performance also saw a sharp drop-off when tackling tasks with $O(n^3)$ complexity compared to $O(n \log n)$ tasks.

Conclusion and Future Work

OpenAI’s o1-preview sets a new benchmark for code execution, demonstrating that advanced reasoning models can effectively "run" code internally. However, the study acknowledges limitations, such as the focus on Python and relatively small dataset size. Future research should expand to lower-level languages like C++, larger datasets across diverse software libraries, and multi-file dependencies to better reflect real-world software engineering environments.

Summary updated

BibTeX
@misc{lyu2024large,
  author = {Chenyang Lyu and Lecheng Yan and Rui Xing and Wenxi Li and Younes Samih and Tianbo Ji and Longyue Wang},
  title = {{Large Language Models as Code Executors: An Exploratory Study}},
  year = {2024},
  eprint = {2410.06667},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.06667},
}
Figure