一句话:研究LLM对教科书级高效代码的技术优化,发现功能保持输出中34%有非平凡加速,但也伴随失败与回退。
运行时效率通常可通过算法选择或实现细节优化提升,但后者依赖难以言传的经验知识。本文检验LLM能否优化按教科书标准已经高效的学生代码。作者用六个LLM优化649个程序,在功能保持的输出中,34%取得非平凡运行时改进,平均执行时间减少18.9%,约合1.3倍加速,说明“教科书高效”与“实际高效”之间存在差距。定性分析显示,LLM能提出更高效API、轻量数据结构和数学简化,但也可能误解需求、引入冗余计算或过度复杂化逻辑,导致33%测试失败和9%性能回退。研究表明LLM有优化潜力,但当前模型可靠应用仍需严格验证与谨慎集成。
Runtime efficiency is an important attribute of source code, typically improved through high-level algorithm selections or low-level implementation optimizations. While algorithmic efficiency is well studied and formally taught, technical optimization on implementation details relies heavily on tacit, experience-driven knowledge that is often inaccessible to novices. To examine this assumption, we prompted six LLMs to optimize 649 student programs that were already efficient by textbook standards. Among the LLM-generated outputs that preserved the original functionality, \(34\%\) achieved non-trivial runtime improvements, with an average execution time reduction of \(18.9\%\) and a corresponding speedup of 1.3 ×. This finding indicates a potential gap between being textbook-efficient and being practically efficient. Our qualitative analysis further reveals both the strengths and limitations of applying LLMs to technical optimizations. On the positive side, LLMs propose more efficient APIs, lightweight data structures, and mathematical simplifications. On the downside, LLMs may misinterpret requirements, introduce redundant computations, or overcomplicate logic, resulting in test failures (\(33\%\)) or performance regressions (\(9\%\)). Overall, our study demonstrates the potential of LLMs to optimize textbook-efficient code. It also highlights, however, that reliable application with current models still requires rigorous verification and careful integration.