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

Create dynamic sitemap on ruby on rails

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site. It’s basically a XML file describing all URLs in your page: The following example shows a Sitemap that contains just one URL and uses all optional tags. The optional tags are in italics. <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">    <url>       <loc>http://www.example.com/</loc>       <lastmod>2005-01-01</lastmod>       <changefreq>monthly</changefreq>     ...

Omniauth Linked in Ruby On Rails

def get_linkedin_user_data      omniauth = request.env["omniauth.auth"]      dat=omniauth.extra.raw_info      linked_app_key = "xxxxxxx"      linkedin_secret_key = "yyyyyyy"      client = LinkedIn::Client.new(linked_app_key,linkedin_secret_key)      client.authorize_from_access(omniauth['credentials']['token'],omniauth['credentials']['secret'])      connections=client.connections(:fields => ["id", "first-name", "last-name","picture-url"])      uid=omniauth['uid']      token=omniauth["credentials"]["token"]      secret=omniauth["credentials"]["secret"]   #linked user data     omniauth = request.env["omniauth.auth"]      data             = omniauth.info      user_name...

Install Rvm on ubuntu

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.0.0-p645 rvm use 2.0.0-p645 --default ruby -v rvm gemset create rails3.2.8 rvm gemset use rails3.2.8 gem install rails -v 3.2.8