arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2503.20126cs.SEcs.AIcs.PF
收藏

我们能让代码更绿色吗?理解大语言模型与人类代码优化中的权衡

Can We Make Code Green? Understanding Trade-Offs in LLMs vs. Human Code Optimizations

University of Zurich(苏黎世大学);University of Bern(伯尔尼大学);Delft University of Technology(代尔夫特理工大学)

Pooja Rani, Jan-Andrea Bard, June Sallou, Alexander Boll, Timo Kehrer, Alberto Bacchelli

AI总结:

本研究评估大语言模型与人类开发者对Matlab代码的能源优化效果,发现LLMs虽提出广泛改进建议,但能源聚焦优化对内存有负面影响且无时间或能源收益,呼吁优先评估编码实践的绿色性。

摘要:

技术的快速演进加速了各领域和用例的软件开发,同时也导致了全球碳排放份额的持续增长。尽管近期的大语言模型(LLMs)声称能协助开发者优化代码以提升性能和能源效率,但其在真实世界场景中的有效性仍有待探索。在本研究中,我们探讨了LLMs在减少真实世界项目环境足迹方面的有效性,重点关注用Matlab编写的软件——Matlab在学术界和工业界广泛用于科学和工程应用。我们分析了100个顶级GitHub仓库中400个脚本的能源聚焦优化。我们考察了由领先LLMs(如GPT-3、GPT-4、Llama和Mixtral)以及一位资深Matlab开发者推荐的2,176项潜在优化,涉及能源消耗、内存使用、执行时间消耗和代码正确性。该开发者作为真实世界的基线,用于比较典型的人类与LLM生成的优化。将这些优化映射到13个高层次主题后,我们发现LLMs提出了广泛的改进——超越能源效率——包括提升代码可读性和可维护性、内存管理、错误处理,而开发者则忽视了一些并行处理、错误处理等。然而,我们的统计测试揭示,能源聚焦的优化意外地对内存使用产生了负面影响,在执行时间或能源消耗方面没有明显益处。我们对能源-时间权衡的定性分析表明,某些主题(如向量化预分配)是形成这些权衡的常见主题之一。随着LLMs在现代软件开发中变得无处不在,我们的研究是一个行动号召:优先评估常见编码实践以识别绿色实践。

英文摘要:

The rapid technological evolution has accelerated software development for various domains and use cases, contributing to a growing share of global carbon emissions. While recent large language models (LLMs) claim to assist developers in optimizing code for performance and energy efficiency, their efficacy in real-world scenarios remains under exploration. In this work, we explore the effectiveness of LLMs in reducing the environmental footprint of real-world projects, focusing on software written in Matlab-widely used in both academia and industry for scientific and engineering applications. We analyze energy-focused optimization on 400 scripts across 100 top GitHub repositories. We examine potential 2,176 optimizations recommended by leading LLMs, such as GPT-3, GPT-4, Llama, and Mixtral, and a senior Matlab developer, on energy consumption, memory usage, execution time consumption, and code correctness. The developer serves as a real-world baseline for comparing typical human and LLM-generated optimizations. Mapping these optimizations to 13 high-level themes, we found that LLMs propose a broad spectrum of improvements--beyond energy efficiency--including improving code readability and maintainability, memory management, error handling while the developer overlooked some parallel processing, error handling etc. However, our statistical tests reveal that the energy-focused optimizations unexpectedly negatively impacted memory usage, with no clear benefits regarding execution time or energy consumption. Our qualitative analysis of energy-time trade-offs revealed that some themes, such as vectorization preallocation, were among the common themes shaping these trade-offs. With LLMs becoming ubiquitous in modern software development, our study serves as a call to action: prioritizing the evaluation of common coding practices to identify the green ones.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑