arXiv:2508.13666cs.SEcs.AI2025-08中稿 · ICSE'26被引 17

去除代码格式可节省24.5%的LLM输入令牌,提升效率。

The Hidden Cost of Readability: How Code Formatting Silently Consumes Your LLM Budget

  • 移除缩进和换行等格式符号,减少输入令牌数。
  • 性能几乎不变,输出令牌减少仅1.2%。
  • 适合需要降低LLM成本的工程部署场景。

源代码通常使用缩进、换行等格式元素提升人类可读性,但这些元素对大语言模型(LLMs)无益,反而增加计算成本与响应时间。本文通过在四种编程语言(Java、Python、C++、C#)和十种LLM(含商业与开源模型)上开展大规模实验,系统评估代码格式对填空式代码补全任务的影响。结果表明,移除格式后,平均输入令牌减少24.5%,输出令牌仅减少1.2%,性能基本保持不变。进一步发现,提示工程与微调可使输出代码长度减少最多达36.1%而不影响正确性。为此,我们开发了一个双向代码转换工具,可无缝集成至现有LLM推理流程,兼顾人类可读性与模型效率。

原文摘要 · Abstract (English)

Source code is usually formatted with elements like indentation and newlines to improve readability for human developers. However, these visual aids do not seem to be beneficial for large language models (LLMs) in the same way since the code is processed as a linear sequence of tokens. Furthermore, these additional tokens can lead to increased computational costs and longer response times for LLMs. If such formatting elements are non-essential to LLMs, we can reduce such costs by removing them from the code. To figure out the role played by formatting elements, we conduct a comprehensive empirical study to evaluate the impact of code formatting on LLM performance and efficiency. Through large-scale experiments on Fill-in-the-Middle Code Completion tasks across four programming languages (Java, Python, C++, C\#) and ten LLMs-including both commercial and open-source models-we systematically analyze token count and performance when formatting elements are removed. Key findings indicate that LLMs can maintain performance across formatted code and unformatted code, achieving an average input token reduction of 24.5\% with negligible output token reductions. This makes code format removal a practical optimization strategy for improving LLM efficiency. Further exploration reveals that both prompting and fine-tuning LLMs can lead to significant reductions (up to 36.1\%) in output code length without compromising correctness. To facilitate practical applications, we develop a bidirectional code transformation tool for format processing, which can be seamlessly integrated into existing LLM inference workflows, ensuring both human readability and LLM efficiency.

LLM优化代码生成效率提升

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。