How to use Benchmark Method in Ruby

Benchmark is an important method in Ruby. It is used to measure and report the time used to execute a Ruby code. To use benchmark in Ruby, you can use benchmark.measure {} method.

Benchmark {} Method in Ruby

benchmark {} function exists in Standard Benchmark Library of Ruby. The purpose of this function is to show the total time elapsed in executing a Ruby code. For example:

# import benchmark library
require 'benchmark'
puts Benchmark.measure { 10_000_00 .times { } }

User CPU Time
System CPU Time
Total of Both
Total Real Time

NOTE: In the above code, it shows the user CPU time, system CPU time, the total of both of the times and the real executing time for a given Ruby code. The above result is in seconds.
Mostly, total real time is the essential one. It tells us how long it really took to execute the code.


Syntax of benchmark {} in Ruby

The syntax of benchmark {} function in Ruby is benchmark.measure {}


Return Value of Benchmark { } in Ruby

Benchmark { } function will return the report in which it shows the total time (in seconds) used to execute a Ruby code.


How to get the Executing Time of a Code in Ruby?

# import benchmark library
require 'benchmark'
begin_time =
sleep 0.3
end_time =
puts "Time taken is #{end_time - begin_time}."

Time taken is 0.300159877.

The sleep method is used to interrupt the current thread for some amount of time. Have a look at the simple way to measure the real time that the function will take to execute a code:


Get the Executing Time for a Block of Code In Ruby

# import benchmark library
require 'benchmark'
puts Benchmark.measure { 552299.times { 1.15129 * 5.686167 } }


0.047000 0.000000 0.047000 (0.035083)

NOTE: The measuring method provides you the time elapsed to execute the block of code. It already exists in the Ruby standard library.


Measure Method of Benchmark Class in Ruby

# import benchmark library
require 'benchmark'
x = 40000
time = Benchmark.measure do
x.times { a = "10" }
puts time


0.000000 0.000000 0.000000 ( 0.005182)


Use of Benchmark bm Method in Ruby

A simple interface to the benchmark method, bm generates subsequent reports with labels and labels parameters have the similar meaning as for benchmark.


See the Example Below for bm Method in Ruby


import benchmark library
require 'benchmark'
n = 3000000 do |x| { for i in 10..n; a = "10"; end } { n.times do   ; a = "10"; end } { 10.upto(n) do ; a = "10"; end }


User System total real
0.328000 0.000000 0.328000 (  0.338789)
0.328000 0.000000 0.328000 (  0.331994)
0.344000 0.000000 0.344000 (  0.339183)

NOTE: If you want to put a label to each of the above lines you can do it as shown in the below example:

require 'benchmark'

n = 3000000 do |x|"first_line:")   { for i in 10..n; a = "10"; end }"second_line:") { n.times do   ; a = "10"; end }"third_line:")  { 10.upto(n) do ; a = "10"; end }


Label User System total real
first_line: 0.328000 0.000000 0.328000 ( 0.338789)
second_line: 0.328000 0.000000 0.328000 ( 0.331994)
third_line: 0.344000 0.000000 0.344000 ( 0.339183)

You request the Benchmark #bm method passing a block. It is a special object produced by the Benchmark. It provides you a reporting method of code that you want to measure. It then runs both of the blocks and prints the result alongside each other.

Note: If the block returns an array of Benchmark :: Tms objects, this will be used to format additional lines of output. If labels parameters are given, these are used to label these extra lines.


Use of Benchmark bmbm Method in Ruby

The Benchmark #bm is really the core of Benchmark, but one more method which is Benchmark #bmbm. It is the same method name with bm twice.

Occasionally, with a benchmark, that generates much stuff and due to this, results start to twist because of exchanges with Ruby’s memory distribution or trash collection routines. During generating objects one block needs to run with the trash collections while other do not and sometimes one block get stuck in it.

And when benchmark starts producing uneven results this is the time to use Benchmark #bmbm. The use of bmbm twice actually benchmarks the code twice. At First, it runs the code as a rehearsal and secondly for the real. This is how the system fully initialized and benchmarks work fairly. For example:

# import benchmark library
require 'benchmark'
test = (1..70000).map { rand }
Benchmark.bmbm do |a|"sort!") { test.dup.sort! }"sort")  { test.dup.sort  }


Rehearsal -----------------------------------------
sort! 0.063000 0.000000 0.063000 ( 0.066066)
sort 0.062000 0.000000 0.062000 ( 0.061603)
-------------------------------- total: 0.125000sec

user system total real
sort! 0.063000 0.000000 0.063000 ( 0.064283)
sort 0.062000 0.000000 0.062000 ( 0.061686)


Use of benchmark-ips Method in Ruby

The benchmark-ips gem offers even so many other features also to use this install the ips gem in ruby.

# import benmcark/ips library
require 'benchmark/ips'
Benchmark.ips do |a|"First: ")        { fib_dp(50) }"Second: ") { fib_rec(40) }!


Calculating -------------------------------------
dp: 5.600k i/100ms
recursive: 1.000 i/100ms
dp: 60.299k (± 2.0%) i/s - 302.400k
recursive: 0.517 (± 0.0%) i/s - 3.000 in 5.800686s

dp: : 60299.5 i/s
recursive: : 0.5 i/s – 116590.15x sloyour