Table of Contents
Fetching ...

Large Language Models as Code Executors: An Exploratory Study

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

TL;DR

This work investigates whether large language models can act as direct code executors by evaluating a suite of code snippets. It introduces Iterative Instruction Prompting (IIP), a line-by-line execution approach that refines outputs and improves accuracy, especially for weaker systems. The study uses 200 CN/EN LeetCode-derived snippets and tests across multiple entrants including o1, GPT-4o, GPT-3.5, DeepSeek-Coder, and Qwen-Coder. Key findings show o1-Preview achieving top accuracies (>90%), IIP boosting weaker performers by about 7.22% on average (up to 18.96%), and that longer code and higher computational complexity can reduce execution success, underscoring the potential and challenges of automated code execution and causality understanding.

Abstract

The capabilities of Large Language Models (LLMs) have significantly evolved, extending from natural language processing to complex tasks like code understanding and generation. We expand the scope of LLMs' capabilities to a broader context, using LLMs to execute code snippets to obtain the output. This paper pioneers the exploration of LLMs as code executors, where code snippets are directly fed to the models for execution, and outputs are returned. We are the first to comprehensively examine this feasibility across various LLMs, including OpenAI's o1, GPT-4o, GPT-3.5, DeepSeek, and Qwen-Coder. Notably, the o1 model achieved over 90% accuracy in code execution, while others demonstrated lower accuracy levels. Furthermore, we introduce an Iterative Instruction Prompting (IIP) technique that processes code snippets line by line, enhancing the accuracy of weaker models by an average of 7.22% (with the highest improvement of 18.96%) and an absolute average improvement of 3.86% against CoT prompting (with the highest improvement of 19.46%). Our study not only highlights the transformative potential of LLMs in coding but also lays the groundwork for future advancements in automated programming and the completion of complex tasks.

Large Language Models as Code Executors: An Exploratory Study

TL;DR

This work investigates whether large language models can act as direct code executors by evaluating a suite of code snippets. It introduces Iterative Instruction Prompting (IIP), a line-by-line execution approach that refines outputs and improves accuracy, especially for weaker systems. The study uses 200 CN/EN LeetCode-derived snippets and tests across multiple entrants including o1, GPT-4o, GPT-3.5, DeepSeek-Coder, and Qwen-Coder. Key findings show o1-Preview achieving top accuracies (>90%), IIP boosting weaker performers by about 7.22% on average (up to 18.96%), and that longer code and higher computational complexity can reduce execution success, underscoring the potential and challenges of automated code execution and causality understanding.

Abstract

The capabilities of Large Language Models (LLMs) have significantly evolved, extending from natural language processing to complex tasks like code understanding and generation. We expand the scope of LLMs' capabilities to a broader context, using LLMs to execute code snippets to obtain the output. This paper pioneers the exploration of LLMs as code executors, where code snippets are directly fed to the models for execution, and outputs are returned. We are the first to comprehensively examine this feasibility across various LLMs, including OpenAI's o1, GPT-4o, GPT-3.5, DeepSeek, and Qwen-Coder. Notably, the o1 model achieved over 90% accuracy in code execution, while others demonstrated lower accuracy levels. Furthermore, we introduce an Iterative Instruction Prompting (IIP) technique that processes code snippets line by line, enhancing the accuracy of weaker models by an average of 7.22% (with the highest improvement of 18.96%) and an absolute average improvement of 3.86% against CoT prompting (with the highest improvement of 19.46%). Our study not only highlights the transformative potential of LLMs in coding but also lays the groundwork for future advancements in automated programming and the completion of complex tasks.

Paper Structure

This paper contains 16 sections, 4 equations, 6 figures, 3 tables.

Figures (6)

  • Figure 1: An illustration to the use of LLMs for code execution as a proxy for evaluating execution competence as well as the implications for understanding outcomes, and progressing towards real-world causality comprehension.
  • Figure 2: Comparison of prompting methods for LLM code execution: vanilla prompting for general guidance, Chain-of-Thought (CoT) prompting for step-by-step analysis, and iterative instruction prompting for refining outputs based on prior responses.
  • Figure 3: Split of average accuracy of all LLMs across different categories for both EN and CN code.
  • Figure 4: Relationship between human pass rates and model accuracy for EN (left) and CN (right) code snippets, the Spearman’s Correlation for EN and CN data are 0.25 and 0.17 respectively.
  • Figure 5: Average accuracy on CN and EN code snippets divided by corresponding computational complexity.
  • ...and 1 more figures