你的AI能用汇编语言重写代码吗?
Can your AI rewrite your code in assembly?

原始链接: https://lemire.me/blog/2026/04/05/can-your-ai-rewrite-your-code-in-assembly/

本文详细描述了一个实验,作者使用人工智能(Grok和Claude)来优化一个简单的C++函数:计算一组字符串中字符('!')出现的次数。最初的C++实现使用`std::count`,每个字符串需要大约1200条指令。 通过迭代提示,人工智能生成了越来越优化的ARM64汇编代码。Claude和Grok逐步采用了基本循环、SIMD指令(NEON)以及更大的数据块处理(高达64字节),并改进了累加器处理。 最终人工智能生成的汇编版本显著减少了指令数量,降至每个字符串约154条——提高了八倍。作者发现他们可以将最佳汇编代码转换回使用SIMD内在函数的C++代码,从而无需*保留*汇编代码。 该实验提出了一个问题:人工智能能否发现超越传统编译器或人类程序员的优化方法,并表明,对于这项特定任务,人工智能目前可以胜过标准的C++编译器。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 你的AI能将你的代码重写成汇编语言吗? (lemire.me) 3点 由 signa11 35分钟前 | 隐藏 | 过去 | 收藏 | 讨论 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

Suppose you have several strings and you want to count the number of instances of the character ! in your strings. In C++, you might solve the problem as follows if you are an old-school programmer.

size_t c = 0;
for (const auto &str : strings) {
    c += std::count(str.begin(), str.end(), '!');
}

You can also get fancier with ranges.

for (const auto &str : strings) {
    c += std::ranges::count(str, '!');
}

And so forth.

But what if you want to go faster? Maybe you’d want to rewrite this function in assembly. I decided to do so, and to have fun using both Grok and Claude as my AIs, setting up a friendly competition.

I started with my function and then I asked AIs to optimize it in assembly. Importantly, they knew which machine I was on, so they started to write ARM assembly.

By repeated prompting, I got the following functions.

  • count_classic: Uses C++ standard library std::count for reference.
  • count_assembly: A basic ARM64 assembly loop (byte-by-byte comparison). Written by Grok.
  • count_assembly_claude: Claude’s SIMD-optimized version using NEON instructions (16-byte chunks).
  • count_assembly_grok: Grok’s optimized version (32-byte chunks).
  • count_assembly_claude_2: Claude’s further optimized version (64-byte chunks with multiple accumulators).
  • count_assembly_grok_2: Grok’s latest version (64-byte chunks with improved accumulator handling).
  • count_assembly_claude_3: Claude’s most advanced version with additional optimizations.

You get the idea.

So, how is the performance? I use random strings of up to 1 kilobyte. In all cases, I test that the functions provide the correct count. I did not closely examine the code, so it is possible that mistakes could be hiding in the code.

I record the average number of instructions per string.

name instructions/string
classic C++ 1200
claude assembly 250
grok assembly 204
claude assembly 2 183
grok assembly 2 176
claude assembly 3 154

By repeated optimization, I reduced the number of instructions by a factor of eight. The running time decreases similarly.

Can we get the AIs to rewrite the best option in C? Yes, although you need SIMD intrinsics. So there is no benefit to leaving the code in assembly in this instance.

An open question is whether the AIs could find optimizations that are not possible if we use a higher-level language like C or C++. It is an intriguing question that I will seek to answer later. For the time being, the AIs can beat my C++ compiler!

My source code is available.

`; modal.addEventListener('click', function(e) { if (e.target === modal) modal.close(); }); modal.querySelector('#bibtex-copy-btn').addEventListener('click', function() { const text = modal.querySelector('#bibtex-target').textContent; navigator.clipboard.writeText(text).then(() => { const origText = this.innerText; this.innerText = "Copied!"; setTimeout(() => this.innerText = origText, 1500); }); }); document.body.appendChild(modal); const style = document.createElement('style'); style.innerHTML = `dialog::backdrop { background: rgba(0, 0, 0, 0.5); }`; document.head.appendChild(style); }                         // 1. Extract the URL             const fullLinkHtml = el.dataset.fullLink;              const tempDiv = document.createElement('div');             tempDiv.innerHTML = fullLinkHtml;              const linkElement = tempDiv.querySelector('a');             const rawUrl = linkElement ? linkElement.href : '';                           // 2. Compute the current access date             const accessedDate = this.getCurrentAccessedDate();              // 3. --- NEW LOGIC: Extract ONLY the year (YYYY) ---             // Gets the full date string, e.g., "November 23, 2025"             const fullDateString = el.dataset.year;             // Use regex to find the four-digit year at the end of the string             const match = fullDateString.match(/(\d{4})$/);             const publicationYear = match ? match[0] : '????'; // e.g., '2025'                          // 4. Generate BibTeX Data with the corrected year             const safeTitle = el.dataset.title.replace(/[^a-zA-Z0-9]/g, '').substring(0, 15);             // Use the clean year for the BibKey             const bibKey = (publicationYear + safeTitle);             const content = `@misc{${bibKey}, author = {${el.dataset.author}}, title = {{${el.dataset.title}}}, year = {${publicationYear}}, howpublished = {\\url{${rawUrl}}}, note = {Accessed: ${accessedDate}} }`;                          // 5. Show Modal             document.getElementById('bibtex-target').textContent = content;             modal.showModal();         }     }; })();
联系我们 contact @ memedata.com