x-i18n: generated_at: “2026-03-01T14:23:37Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 200b43a32e2bef5de723b6211bb0e6b6ca01461093ccb5c277bad1954bad0de0 source_path: ch13-04-performance.md workflow: 16
循环与迭代器的性能比较 (Performance in Loops vs. Iterators)
为了决定使用循环还是迭代器,你需要知道哪种实现更快:是带有显式 for 循环的 search 函数版本,还是使用迭代器的版本。
To determine whether to use loops or iterators, you need to know which
implementation is faster: the version of the search function with an explicit
for loop or the version with iterators.
我们通过将 Arthur Conan Doyle 爵士的《福尔摩斯探案集》全文加载到 String 中,并在内容中搜索单词 the 来运行了一个基准测试。以下是使用 for 循环版本和使用迭代器版本的 search 基准测试结果:
We ran a benchmark by loading the entire contents of The Adventures of
Sherlock Holmes by Sir Arthur Conan Doyle into a String and looking for the
word the in the contents. Here are the results of the benchmark on the
version of search using the for loop and the version using iterators:
test bench_search_for ... bench: 19,620,300 ns/iter (+/- 915,700)
test bench_search_iter ... bench: 19,234,900 ns/iter (+/- 657,200)
两项实现的性能相近!我们不会在这里解释基准测试代码,因为重点不是证明这两个版本是等价的,而是为了大致了解这两项实现在性能方面的比较。
The two implementations have similar performance! We won’t explain the benchmark code here because the point is not to prove that the two versions are equivalent but to get a general sense of how these two implementations compare performance-wise.
为了进行更全面的基准测试,你应该尝试使用各种不同大小的文本作为 contents ,使用不同的单词和不同长度的单词作为 query ,以及各种其他变化。重点在于:迭代器虽然是高级抽象,但会被编译成与你手动编写低级代码大致相同的机器码。迭代器是 Rust 的“零成本抽象 (zero-cost abstractions)”之一,这意味着使用该抽象不会带来额外的运行时开销。这类似于 C++ 的原始设计者和实现者 Bjarne Stroustrup 在其 2012 年 ETAPS 主旨演讲《C++ 基础》(Foundations of C++) 中对“零开销”的定义:
For a more comprehensive benchmark, you should check using various texts of
various sizes as the contents, different words and words of different lengths
as the query, and all kinds of other variations. The point is this:
Iterators, although a high-level abstraction, get compiled down to roughly the
same code as if you’d written the lower-level code yourself. Iterators are one
of Rust’s zero-cost abstractions, by which we mean that using the abstraction
imposes no additional runtime overhead. This is analogous to how Bjarne
Stroustrup, the original designer and implementor of C++, defines
zero-overhead in his 2012 ETAPS keynote presentation “Foundations of C++”:
通常,C++ 的实现遵循零开销原则:你没用到的,你不需要为其付费。更进一步:你用到的,你无法通过手写代码做得更好。
In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for. And further: What you do use, you couldn’t hand code any better.
在许多情况下,使用迭代器的 Rust 代码会编译成与你手动编写的相同的汇编代码。诸如循环展开和消除数组访问的边界检查等优化措施会被应用,使生成的代码极其高效。既然你知道了这一点,你就可以放心大胆地使用迭代器和闭包了!它们让代码看起来级别更高,但这样做并不会带来运行时性能损失。
In many cases, Rust code using iterators compiles to the same assembly you’d write by hand. Optimizations such as loop unrolling and eliminating bounds checking on array access apply and make the resultant code extremely efficient. Now that you know this, you can use iterators and closures without fear! They make code seem like it’s higher level but don’t impose a runtime performance penalty for doing so.
总结 (Summary)
闭包和迭代器是受函数式编程语言思想启发的 Rust 特性。它们有助于 Rust 能够以低级性能清晰地表达高级思想。闭包和迭代器的实现方式使得运行时性能不受影响。这是 Rust 努力提供零成本抽象目标的一部分。
Closures and iterators are Rust features inspired by functional programming language ideas. They contribute to Rust’s capability to clearly express high-level ideas at low-level performance. The implementations of closures and iterators are such that runtime performance is not affected. This is part of Rust’s goal to strive to provide zero-cost abstractions.
现在我们已经提高了 I/O 项目的表达能力,让我们看看 cargo 的更多特性,这些特性将帮助我们将项目分享给世界。
Now that we’ve improved the expressiveness of our I/O project, let’s look at
some more features of cargo that will help us share the project with the
world.