DEV Community

Subash
Subash

Posted on

Ruby Benchmarking

Benchmarking is one of the crucial part of code, since it can help developers to identify and fix the performance issues thereby improving the execution speed of the application. That said will see how can we benchmark a given code

Requirements

benchmark - gem. Rails comes with this gem installed by default. There are some external gems which also serves the same purpose. In this article we will cover the benchmark gem

The usage is simple. Write scripts - load it in system-irb/rails-irb - execute and analyse

Lets benchmark below select and detect methods
launch irb or rails console

# declare an array of 1 to 100
arr = (1...1000)

def get_matching_first_element_using_select(arr)
  return arr.select{|num| num == 1}.first
end

def get_matching_first_element_using_detect(arr)
  return arr.detect{|num| num == 1}
end
Enter fullscreen mode Exit fullscreen mode

It is obvious that detect is much more effecient than select. Lets confirm this with benchmark

Require the gem (if you are launching rails console requiring 'benchmark' is not needed as it is already loaded by default)

require 'benchmark'

Benchmark.measure{ get_matching_first_element_using_select(arr)}
=> 
#<Benchmark::Tms:0x00007fa96a532fd0                                        
 @cstime=0.0,                                                             
 @cutime=0.0,                                                             
 @label="",                                                               
 @real=0.00016800200000943732,                                            
 @stime=1.2999999999985246e-05,                                           
 @total=0.00017499999999998073,                                           
 @utime=0.00016199999999999548> 
Enter fullscreen mode Exit fullscreen mode

If you just want to see the actual time taken in microseconds use ms instead of measure.

Benchmark.ms{ get_matching_first_element_using_select(arr)}
=> 0.16017500001908047 #we will get this simplified clutter free output
Enter fullscreen mode Exit fullscreen mode

This is fine but we need to do benchmark for 2 different methods separately. Is there any way to run both of them together and compare side by side. Yes there is

Benchmark.bm do |x|
  x.report("select: ") {100.times do; get_matching_first_element_using_select(arr); end}
  x.report("detect: ") {100.times do; get_matching_first_element_using_detect(arr); end} 
end
Enter fullscreen mode Exit fullscreen mode

This will yeild the below output which is easy to compare

              user     system      total        real
select:   0.004269   0.000015   0.004284 (  0.004280)
detect:   0.000052   0.000003   0.000055 (  0.000055)

Enter fullscreen mode Exit fullscreen mode

And the results are clear, Detect is faster than select since we are fetching the first element - which is like 78 times faster.

There are multiple ways to benchmark, this is just the basic. Read more at source

Top comments (0)