The Benchmark module provides methods for benchmarking Ruby code, giving detailed reports on the time taken for each task.
Installation
You can install it as a gem:
1
$ gem install benchmark
or add it into a Gemfile (Bundler):
1 2 3 4 5 6 7 8
# Gemfile
# Put gems used only for development or testing in the appropriate group in the Gemfile group :developmentdo # ruby/benchmark: The Benchmark module provides methods for benchmarking Ruby code, giving detailed reports on the time taken for each task. # https://github.com/ruby/benchmark gem 'benchmark', '0.1.1' end
Then, run bundle install.
1
$ bundle install
Usage
The Benchmark module provides methods to measure and report the time used to execute Ruby code.
Measure the time to construct the string given by the expression “a”*1_000_000_000:
On my machine (OSX 10.8.3 on i5 1.7 GHz) this generates:
1
0.3500000.4000000.750000 ( 0.835234)
This report shows the user CPU time, system CPU time, the sum of the user and system CPU times, and the elapsed real time. The unit of time is seconds.
Do some experiments sequentially using the #bm method:
1 2 3 4 5 6 7
require'benchmark' n = 5000000 Benchmark.bm do |x| x.report { for i in1..n; a = "1"; end } x.report { n.times do ; a = "1"; end } x.report { 1.upto(n) do ; a = "1"; end } end
The result:
1 2 3 4
user system total real 1.0100000.0000001.010000 ( 1.014479) 1.0000000.0000001.000000 ( 0.998261) 0.9800000.0000000.980000 ( 0.981335)
Continuing the previous example, put a label in each report:
1 2 3 4 5 6 7
require'benchmark' n = 5000000 Benchmark.bm(7) do |x| x.report("for:") { for i in1..n; a = "1"; end } x.report("times:") { n.times do ; a = "1"; end } x.report("upto:") { 1.upto(n) do ; a = "1"; end } end
The result:
1 2 3 4
user system total real for:1.0100000.0000001.010000 ( 1.015688) times:1.0000000.0000001.000000 ( 1.003611) upto:1.0300000.0000001.030000 ( 1.028098)
The times for some benchmarks depend on the order in which items are run. These differences are due to the cost of memory allocation and garbage collection. To avoid these discrepancies, the #bmbm method is provided. For example, to compare ways to sort an array of floats:
1 2 3 4 5 6
require'benchmark' array = (1..1000000).map { rand } Benchmark.bmbm do |x| x.report("sort!") { array.dup.sort! } x.report("sort") { array.dup.sort } end
The result:
1 2 3 4 5 6 7
Rehearsal ----------------------------------------- sort! 1.4900000.0100001.500000 ( 1.490520) sort 1.4600000.0000001.460000 ( 1.463025) -------------------------------- total:2.960000sec user system total real sort! 1.4600000.0000001.460000 ( 1.460465) sort 1.4500000.0100001.460000 ( 1.448327)
Report statistics of sequential experiments with unique labels, using the #benchmark method:
1 2 3 4 5 6 7 8 9
require'benchmark' includeBenchmark# we need the CAPTION and FORMAT constants n = 5000000 Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x| tf = x.report("for:") { for i in1..n; a = "1"; end } tt = x.report("times:") { n.times do ; a = "1"; end } tu = x.report("upto:") { 1.upto(n) do ; a = "1"; end } [tf+tt+tu, (tf+tt+tu)/3] end
The result:
1 2 3 4 5 6
user system total real for:0.9500000.0000000.950000 ( 0.952039) times:0.9800000.0000000.980000 ( 0.984938) upto:0.9500000.0000000.950000 ( 0.946787) >total:2.8800000.0000002.880000 ( 2.883764) >avg:0.9600000.0000000.960000 ( 0.961255)