Skip to content

Commit ff8b2b0

Browse files
committed
generate
1 parent 2492744 commit ff8b2b0

File tree

2 files changed

+607
-239
lines changed

2 files changed

+607
-239
lines changed

public/1.6/book/benchmark-tests.html

Lines changed: 130 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta name="generator" content="rustdoc">
7-
<title>Benchmark tests</title>
7+
<title>ベンチマークテスト</title>
88

99
<link rel="stylesheet" type="text/css" href="rustbook.css">
1010

@@ -185,9 +185,15 @@
185185
<div id='page'>
186186

187187

188-
<h1 class="title">Benchmark tests</h1>
189-
<p>Rust supports benchmark tests, which can test the performance of your
190-
code. Let&#39;s make our <code>src/lib.rs</code> look like this (comments elided):</p>
188+
<h1 class="title">ベンチマークテスト</h1>
189+
<!-- % Benchmark tests -->
190+
191+
<!-- Rust supports benchmark tests, which can test the performance of your -->
192+
193+
<!-- code. Let's make our `src/lib.rs` look like this (comments elided): -->
194+
195+
<p>Rustはコードのパフォーマンスをテストできるベンチマークテストをサポートしています。
196+
早速、 <code>src/lib.rc</code> を以下のように作っていきましょう(コメントは省略しています):</p>
191197
<span class='rusttest'>#![feature(test)]
192198

193199
fn main() {
@@ -237,15 +243,28 @@ <h1 class="title">Benchmark tests</h1>
237243
}
238244
}</pre>
239245

240-
<p>Note the <code>test</code> feature gate, which enables this unstable feature.</p>
246+
<!-- Note the `test` feature gate, which enables this unstable feature. -->
247+
248+
<p>不安定なベンチマークのフィーチャを有効にするため、 <code>test</code> フィーチャゲートを利用していることに注意して下さい。</p>
249+
250+
<!-- We've imported the `test` crate, which contains our benchmarking support. -->
251+
252+
<!-- We have a new function as well, with the `bench` attribute. Unlike regular -->
241253

242-
<p>We&#39;ve imported the <code>test</code> crate, which contains our benchmarking support.
243-
We have a new function as well, with the <code>bench</code> attribute. Unlike regular
244-
tests, which take no arguments, benchmark tests take a <code>&amp;mut Bencher</code>. This
245-
<code>Bencher</code> provides an <code>iter</code> method, which takes a closure. This closure
246-
contains the code we&#39;d like to benchmark.</p>
254+
<!-- tests, which take no arguments, benchmark tests take a `&mut Bencher`. This -->
247255

248-
<p>We can run benchmark tests with <code>cargo bench</code>:</p>
256+
<!-- `Bencher` provides an `iter` method, which takes a closure. This closure -->
257+
258+
<!-- contains the code we'd like to benchmark. -->
259+
260+
<p>ベンチマークテストのサポートを含んだ <code>test</code> クレートをインポートしています。
261+
また、 <code>bench</code> アトリビュートのついた新しい関数を定義しています。
262+
引数を取らない通常のテストとは異なり、ベンチマークテストは <code>&amp;mut Bencher</code> を引数に取ります。
263+
<code>Bencher</code> はベンチマークしたいコードを含んだクロージャを引数に取る <code>iter</code> メソッドを提供しています。</p>
264+
265+
<!-- We can run benchmark tests with `cargo bench`: -->
266+
267+
<p>ベンチマークテストは以下のように <code>cargo bench</code> のようにして実施できます:</p>
249268

250269
<pre><code class="language-bash">$ cargo bench
251270
Compiling adder v0.0.1 (file:///home/steve/tmp/adder)
@@ -258,31 +277,66 @@ <h1 class="title">Benchmark tests</h1>
258277
test result: ok. 0 passed; 0 failed; 1 ignored; 1 measured
259278
</code></pre>
260279

261-
<p>Our non-benchmark test was ignored. You may have noticed that <code>cargo bench</code>
262-
takes a bit longer than <code>cargo test</code>. This is because Rust runs our benchmark
263-
a number of times, and then takes the average. Because we&#39;re doing so little
264-
work in this example, we have a <code>1 ns/iter (+/- 0)</code>, but this would show
265-
the variance if there was one.</p>
280+
<!-- Our non-benchmark test was ignored. You may have noticed that `cargo bench` -->
281+
282+
<!-- takes a bit longer than `cargo test`. This is because Rust runs our benchmark -->
283+
284+
<!-- a number of times, and then takes the average. Because we're doing so little -->
285+
286+
<!-- work in this example, we have a `1 ns/iter (+/- 0)`, but this would show -->
287+
288+
<!-- the variance if there was one. -->
289+
290+
<p>ベンチマークでないテストは無視されます。
291+
<code>cargo bench</code><code>cargo test</code> よりも時間がかかることにお気づきになったかもしれません。
292+
これは、Rustがベンチマークをかなりの回数繰り返し実行し、その結果の平均を取るためです。
293+
今回のコードでは非常に小さな処理しか行っていないために、 <code>1 ns/iter (+/- 0)</code> という結果を得ました、
294+
しかし、この結果は変動することがあるでしょう。</p>
295+
296+
<!-- Advice on writing benchmarks: -->
297+
298+
<p>以下は、ベンチマークを書くときのアドバイスです:</p>
299+
300+
<!-- * Move setup code outside the `iter` loop; only put the part you want to measure inside -->
301+
302+
<!-- * Make the code do "the same thing" on each iteration; do not accumulate or change state -->
303+
304+
<!-- * Make the outer function idempotent too; the benchmark runner is likely to run -->
305+
306+
<!-- it many times -->
307+
308+
<!-- * Make the inner `iter` loop short and fast so benchmark runs are fast and the -->
309+
310+
<!-- calibrator can adjust the run-length at fine resolution -->
266311

267-
<p>Advice on writing benchmarks:</p>
312+
<!-- * Make the code in the `iter` loop do something simple, to assist in pinpointing -->
313+
314+
<!-- performance improvements (or regressions) -->
268315

269316
<ul>
270-
<li>Move setup code outside the <code>iter</code> loop; only put the part you want to measure inside</li>
271-
<li>Make the code do &quot;the same thing&quot; on each iteration; do not accumulate or change state</li>
272-
<li>Make the outer function idempotent too; the benchmark runner is likely to run
273-
it many times</li>
274-
<li> Make the inner <code>iter</code> loop short and fast so benchmark runs are fast and the
275-
calibrator can adjust the run-length at fine resolution</li>
276-
<li>Make the code in the <code>iter</code> loop do something simple, to assist in pinpointing
277-
performance improvements (or regressions)</li>
317+
<li>セットアップのコードを <code>iter</code> の外に移し、計測したい箇所のみを <code>iter</code> の中に書きましょう。</li>
318+
<li>それぞれの繰り返しでコードが「同じこと」をするようにし、集計をしたり状態を変更したりといったことはしないで下さい。</li>
319+
<li>利用している外部の関数についても冪等にしましょう、ベンチマークはその関数をおそらく何度も実行します。</li>
320+
<li>内側の <code>iter</code> ループを短く高速にしましょう、そうすることでベンチマークの実行は高速になり、キャリブレータは実行の長さをより良い精度で補正できるようになります。</li>
321+
<li>パフォーマンスの向上(または低下)をピンポイントで突き止められるように、<code>iter</code> ループ中のコードの処理を簡潔にしましょう。</li>
278322
</ul>
279323

280-
<h2 id='gotcha-optimizations' class='section-header'><a href='#gotcha-optimizations'>Gotcha: optimizations</a></h2>
281-
<p>There&#39;s another tricky part to writing benchmarks: benchmarks compiled with
282-
optimizations activated can be dramatically changed by the optimizer so that
283-
the benchmark is no longer benchmarking what one expects. For example, the
284-
compiler might recognize that some calculation has no external effects and
285-
remove it entirely.</p>
324+
<!-- ## Gotcha: optimizations -->
325+
326+
<h2 id='注意点-最適化' class='section-header'><a href='#注意点-最適化'>注意点: 最適化</a></h2>
327+
<!-- There's another tricky part to writing benchmarks: benchmarks compiled with -->
328+
329+
<!-- optimizations activated can be dramatically changed by the optimizer so that -->
330+
331+
<!-- the benchmark is no longer benchmarking what one expects. For example, the -->
332+
333+
<!-- compiler might recognize that some calculation has no external effects and -->
334+
335+
<!-- remove it entirely. -->
336+
337+
<p>ベンチマークを書くときに気をつけなければならないその他の点は: 最適化を有効にしてコンパイルしたベンチマークは劇的に最適化され、
338+
もはや本来ベンチマークしたかったコードとは異なるという点です。
339+
たとえば、コンパイラは幾つかの計算がなにも外部に影響を及ぼさないことを認識してそれらの計算を取り除くかもしれません。</p>
286340
<span class='rusttest'>#![feature(test)]
287341

288342
fn main() {
@@ -308,35 +362,53 @@ <h2 id='gotcha-optimizations' class='section-header'><a href='#gotcha-optimizati
308362
});
309363
}</pre>
310364

311-
<p>gives the following results</p>
365+
<!-- gives the following results -->
366+
367+
<p>このベンチマークは以下の様な結果となります</p>
312368

313369
<pre><code class="language-text">running 1 test
314370
test bench_xor_1000_ints ... bench: 0 ns/iter (+/- 0)
315371

316372
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
317373
</code></pre>
318374

319-
<p>The benchmarking runner offers two ways to avoid this. Either, the closure that
320-
the <code>iter</code> method receives can return an arbitrary value which forces the
321-
optimizer to consider the result used and ensures it cannot remove the
322-
computation entirely. This could be done for the example above by adjusting the
323-
<code>b.iter</code> call to</p>
375+
<!-- The benchmarking runner offers two ways to avoid this. Either, the closure that -->
376+
377+
<!-- the `iter` method receives can return an arbitrary value which forces the -->
378+
379+
<!-- optimizer to consider the result used and ensures it cannot remove the -->
380+
381+
<!-- computation entirely. This could be done for the example above by adjusting the -->
382+
383+
<!-- `b.iter` call to -->
384+
385+
<p>ベンチマークランナーはこの問題を避ける2つの手段を提供します。
386+
<code>iter</code> メソッドが受け取るクロージャは任意の値を返すことができ、
387+
オプティマイザに計算の結果が利用されていると考えさせ、その計算を取り除くことができないと保証することができます。
388+
これは、上のコードにおいて <code>b.iter</code> の呼出を以下のようにすることで可能です:</p>
324389
<span class='rusttest'>fn main() {
325390
struct X;
326391
impl X { fn iter&lt;T, F&gt;(&amp;self, _: F) where F: FnMut() -&gt; T {} } let b = X;
327392
b.iter(|| {
328-
// note lack of `;` (could also use an explicit `return`).
393+
// note lack of `;` (could also use an explicit `return`).
394+
// `;` が無いことに注意して下さい (明示的な `return` を使うこともできます)。
329395
(0..1000).fold(0, |old, new| old ^ new)
330396
});
331397
}</span><pre class='rust rust-example-rendered'>
332398
<span class='ident'>b</span>.<span class='ident'>iter</span>(<span class='op'>||</span> {
333-
<span class='comment'>// note lack of `;` (could also use an explicit `return`).</span>
399+
<span class='comment'>// `;` が無いことに注意して下さい (明示的な `return` を使うこともできます)。</span>
334400
(<span class='number'>0</span>..<span class='number'>1000</span>).<span class='ident'>fold</span>(<span class='number'>0</span>, <span class='op'>|</span><span class='ident'>old</span>, <span class='ident'>new</span><span class='op'>|</span> <span class='ident'>old</span> <span class='op'>^</span> <span class='ident'>new</span>)
335401
});</pre>
336402

337-
<p>Or, the other option is to call the generic <code>test::black_box</code> function, which
338-
is an opaque &quot;black box&quot; to the optimizer and so forces it to consider any
339-
argument as used.</p>
403+
<!-- Or, the other option is to call the generic `test::black_box` function, which -->
404+
405+
<!-- is an opaque "black box" to the optimizer and so forces it to consider any -->
406+
407+
<!-- argument as used. -->
408+
409+
<p>もう一つの方法としては、ジェネリックな <code>test::black_box</code> 関数を呼び出すという手段が有ります、
410+
<code>test::black_box</code> 関数はオプティマイザにとって不透明な「ブラックボックス」であり、
411+
オプティマイザに引数のどれもが利用されていると考えさせることができます。</p>
340412
<span class='rusttest'>#![feature(test)]
341413

342414
extern crate test;
@@ -361,20 +433,30 @@ <h2 id='gotcha-optimizations' class='section-header'><a href='#gotcha-optimizati
361433
(<span class='number'>0</span>..<span class='ident'>n</span>).<span class='ident'>fold</span>(<span class='number'>0</span>, <span class='op'>|</span><span class='ident'>a</span>, <span class='ident'>b</span><span class='op'>|</span> <span class='ident'>a</span> <span class='op'>^</span> <span class='ident'>b</span>)
362434
})</pre>
363435

364-
<p>Neither of these read or modify the value, and are very cheap for small values.
365-
Larger values can be passed indirectly to reduce overhead (e.g.
366-
<code>black_box(&amp;huge_struct)</code>).</p>
436+
<!-- Neither of these read or modify the value, and are very cheap for small values. -->
437+
438+
<!-- Larger values can be passed indirectly to reduce overhead (e.g. -->
367439

368-
<p>Performing either of the above changes gives the following benchmarking results</p>
440+
<!-- `black_box(&huge_struct)`). -->
441+
442+
<p>2つの手段のどちらも値を読んだり変更したりせず、小さな値に対して非常に低コストです。
443+
大きな値は、オーバーヘッドを減らすために間接的に渡すことができます(例: <code>black_box(&amp;huge_struct)</code>)。</p>
444+
445+
<!-- Performing either of the above changes gives the following benchmarking results -->
446+
447+
<p>上記のどちらかの変更を施すことでベンチマークの結果は以下のようになります</p>
369448

370449
<pre><code class="language-text">running 1 test
371450
test bench_xor_1000_ints ... bench: 131 ns/iter (+/- 3)
372451

373452
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
374453
</code></pre>
375454

376-
<p>However, the optimizer can still modify a testcase in an undesirable manner
377-
even when using either of the above.</p>
455+
<!-- However, the optimizer can still modify a testcase in an undesirable manner -->
456+
457+
<!-- even when using either of the above. -->
458+
459+
<p>しかしながら、上のどちらかの方法をとったとしても依然オプティマイザはテストケースを望まない形で変更する場合があります。</p>
378460

379461
<script type="text/javascript">
380462
window.playgroundUrl = "https://play.rust-lang.org";

0 commit comments

Comments
 (0)