Skip to main content

Benchmark in Ruby code using bm and bmbm with examples

Benchmark in Ruby code using bm and bmbm with examples?

In Ruby, you have to perform benchmarking to measure performance of your code. Ruby allows you to do Benchmarking using library called 'benchmark'

You have to include library before you start with Benchmarking.

Let us see example of how you can perform benchmarking in Ruby.

require 'benchmark'

It will allow you to use Benchmark library.

Now, suppose you have two code blocks which you have to compare,
Let us say we have array a and b,

> a = []
> b = []

Now, you have to push the element 5 into these arrays. Let's perform this operation 1000000 times so that it will take significant amount of time by which we can compare these two operations or code blocks.

Code block 1:

1000000.times{ a=[]; a << 5}

Code block 2:

1000000.times{ b=[]; a.push(5)}

1. Benchmark bm method

Benchmark.bm do |performance|
performance.report("Insert"){ 1000000.times{ a=[]; a << 5}}
performance.report("Push"){ 1000000.times{ a=[]; a.push(5)}}
end

Output

=>

user     system      total        real

Insert  0.190000   0.000000   0.190000 (  0.197922)

Push  0.210000   0.000000   0.210000 (  0.208997)

So, the Benchmarking performance shows user time, system time, total time and real time taken by code block to execute. As we reported performance of two blocks the result was performance of two blocks.

From the result it is clear that array insert (<<) is faster than array push. Find more about array insert and array push.
You can also perform benchmarking as,
2. Benchmark bmbm method

Benchmark.bmbm do |performance|

performance.report("Insert"){ 1000000.times{ a=[]; a << 5}}

performance.report("Push"){ 1000000.times{ a=[]; a.push(5)}}

end
Output


=>
Rehearsal ------------------------------------------

Insert   0.200000   0.000000   0.200000 (  0.201332)

Push     0.200000   0.000000   0.200000 (  0.201211)

--------------------------------- total: 0.400000sec

user     system      total        real

Insert   0.180000   0.000000   0.180000 (  0.176327)

Push     0.200000   0.000000   0.200000 (  0.199285)

=> [  0.180000   0.000000   0.180000 (  0.176327)

,   0.200000   0.000000   0.200000 (  0.199285)]

Basically, bmbm is used so that Garbage Collection that takes place before each operation should not affect the result and we should get consistent result.
Conclusion

- bmbm performs rehearsal for the first time and then performs actual operation so that we can get consistent results.
- So, by now you would be comfortable with measuring performance of various code blocks if you want.

Comments

Popular posts from this blog

Get the Current url in ruby on rails

request.refererrequest.fullpathrequest.fullpath.split("?")[0]request.referer or request.env['HTTP_REFERER'] your controller to get the referer url. 

--------------------
request.original_url
request.url
request.host_with_port
<link rel="canonical" href="<%= request.url %>" />
<%=request.path%>
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
request.env['REQUEST_URI']

 For Rails 3.2 or Rails 4 you should use request.original_url to get the current URL. More detail.

For Rails 3: You want "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.

For Rails 2: You want request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).?

Gem::LoadError (Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).) In rails?


1)change the gemfile in mysql version
 gem 'mysql2'

2)Change to mysql2 version
 gem 'mysql2','0.3.20'

Active Scaffold : Add radio buttons to your form ruby on rails ?

Active Scaffold : Add radio buttons to your form ruby on rails ?

The way to add radio buttons to your active scaffold create/update from is as shown below

#Controller

class UsersController < ApplicationController
active_scaffold :user do |config|
config.label = “Users”
config.columns = [:name, :gender]
config.columns[:gender].form_ui = :radio
config.columns[:gender].options[:options] = [['Male', '1'], ['Female','2']]
end
end