0046 Benchmark

Need to speed up?

Benchmark allows you to measure the execution time of a block with measure. You’ll find user and system CPU times, as well as the sum of the two. In brackets, the actual time. The unit is seconds. In this example, it’s long but not CPU-intensive.

0046-benchmark_1.png

Comparing implementations is generally more interesting, and can be done with bm or bmbm. The bmbm is run twice to « warm up » the code for a more accurate result. For example, compare flat_map and map { ... }.flatten and check that flat_map is indeed faster.

0046-benchmark_2.png

fast-ruby presents some interesting comparisons, like while vs loop, which do you think is faster?

Link to Ruby documentation for Benchmark.