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.
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.
fast-ruby presents some interesting comparisons, like while
vs loop
, which do you think is faster?
Link to Ruby documentation for Benchmark
.