Simple Benchmarking in Ruby

September 14, 2014

I want to go into a bit of benchmarking. Often on Twitter you will see developers tweet about some statistics around benchmarking that they are doing. So what is the definition of benchmarking? Benchmarking is to evaluate or check (something) by comparison with a standard.

You might want to measure and log execution times to work out what the benchmark of you program is or you could possibly measure the time taken to call a web service, collect this data and then have a way to benchmark performance.

So how can you do this in Ruby? That is the exact question I asked myself; I wanted something simple to measure the User CPU Time, System CPU Time, User + System CPU Time and finally Real Elapsed Time.

Ruby, as usual has us covered. Included in the Standard Library is Benchmark. Therefore, all you need to do is add a require to the benchmark class to pull in all the bits you need.

Here are our requirements, measure the differences between using a for loop, the ‘times’ method and the ‘upto’ method based on 1000000 iterations.

Now drum roll please as this is some hectic code!

          user     system      total        real
for loop  0.050000   0.000000   0.050000 (  0.056730)
times     0.060000   0.000000   0.060000 (  0.055071)
upto      0.050000   0.000000   0.050000 (  0.055136)

Easy right? The ‘bm’ method generates sequential reports with labels. Meaning it will display your reports in the order they were executed and with the text passed into the ‘report’ method.

Real nice how about a more real-life example? Hypothetically speaking let us say we have some code that works out whether or not a number is or is not a prime number. We want to measure the execution of this code run 100000 times.

First here is the code we will use to determine whether the number passed in is a prime number.

Here is the code that will measure the execution time:

This code outputs the following on my machine:

                              user     system      total        real
is prime number over 100000 executions  0.300000   0.000000   0.300000 (  0.295098)

As usual Ruby has us covered with some of it’s awesome built-in functionality. Go forth and benchmark all the things.

Benchmark all the things


Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.

comments powered by Disqus