4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > Benchmark tests </ title >
7
+ < title > ベンチマークテスト </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
188
- < h1 class ="title "> Benchmark tests</ h1 >
189
- < p > Rust supports benchmark tests, which can test the performance of your
190
- code. Let'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 >
191
197
< span class ='rusttest '> #![feature(test)]
192
198
193
199
fn main() {
@@ -237,15 +243,28 @@ <h1 class="title">Benchmark tests</h1>
237
243
}
238
244
}</ pre >
239
245
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 -->
241
253
242
- < p > We'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 > &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'd like to benchmark.</ p >
254
+ <!-- tests, which take no arguments, benchmark tests take a `&mut Bencher`. This -->
247
255
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 > &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 >
249
268
250
269
< pre > < code class ="language-bash "> $ cargo bench
251
270
Compiling adder v0.0.1 (file:///home/steve/tmp/adder)
@@ -258,31 +277,66 @@ <h1 class="title">Benchmark tests</h1>
258
277
test result: ok. 0 passed; 0 failed; 1 ignored; 1 measured
259
278
</ code > </ pre >
260
279
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'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 -->
266
311
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) -->
268
315
269
316
< 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 "the same thing" 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 >
278
322
</ ul >
279
323
280
- < h2 id ='gotcha-optimizations ' class ='section-header '> < a href ='#gotcha-optimizations '> Gotcha: optimizations</ a > </ h2 >
281
- < p > There'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 >
286
340
< span class ='rusttest '> #![feature(test)]
287
341
288
342
fn main() {
@@ -308,35 +362,53 @@ <h2 id='gotcha-optimizations' class='section-header'><a href='#gotcha-optimizati
308
362
});
309
363
}</ pre >
310
364
311
- < p > gives the following results</ p >
365
+ <!-- gives the following results -->
366
+
367
+ < p > このベンチマークは以下の様な結果となります</ p >
312
368
313
369
< pre > < code class ="language-text "> running 1 test
314
370
test bench_xor_1000_ints ... bench: 0 ns/iter (+/- 0)
315
371
316
372
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
317
373
</ code > </ pre >
318
374
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 >
324
389
< span class ='rusttest '> fn main() {
325
390
struct X;
326
391
impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
327
392
b.iter(|| {
328
- // note lack of `;` (could also use an explicit `return`).
393
+ // note lack of `;` (could also use an explicit `return`).
394
+ // `;` が無いことに注意して下さい (明示的な `return` を使うこともできます)。
329
395
(0..1000).fold(0, |old, new| old ^ new)
330
396
});
331
397
}</ span > < pre class ='rust rust-example-rendered '>
332
398
< 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 >
334
400
(< 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 > )
335
401
});</ pre >
336
402
337
- < p > Or, the other option is to call the generic < code > test::black_box</ code > function, which
338
- is an opaque "black box" 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 >
340
412
< span class ='rusttest '> #![feature(test)]
341
413
342
414
extern crate test;
@@ -361,20 +433,30 @@ <h2 id='gotcha-optimizations' class='section-header'><a href='#gotcha-optimizati
361
433
(< 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 > )
362
434
})</ pre >
363
435
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(&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. -- >
367
439
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(&huge_struct)</ code > )。</ p >
444
+
445
+ <!-- Performing either of the above changes gives the following benchmarking results -->
446
+
447
+ < p > 上記のどちらかの変更を施すことでベンチマークの結果は以下のようになります</ p >
369
448
370
449
< pre > < code class ="language-text "> running 1 test
371
450
test bench_xor_1000_ints ... bench: 131 ns/iter (+/- 3)
372
451
373
452
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
374
453
</ code > </ pre >
375
454
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 >
378
460
379
461
< script type ="text/javascript ">
380
462
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments