Learning to code is a path full of struggles, and learning Ruby isn’t the exception. But you’ll agree with me, that practice is the best way to learn and develop your skills.

Out there are plenty of sites to choose from where you can achieve this. But after n amount of solved challenges, you’ll start questioning your solutions.

You’ll start feeling, that is not enough to come up with an answer to the challenge. And you’ll be wondering if there is a better, faster, and less consuming memory approach.

If this is your case, if you have reached this stage, then you’re more than ready to begin benchmarking your code. And if you’re not, well, let me tell you that learning this will step up your game.

So, what is this “benchmarking” thing anyway?..

Benchmarking is the process of measuring the performance of something… in this case the performance of your code.

Benchmarking is the process of measuring the performance of something.

And that’s it, there is nothing more to add.

Yeah, I know what you’re thinking, that knowing the meaning doesn’t serve your purpose. But I’m positive that at least it will give you a broad idea.

With this in mind, the next question to answer is, “How benchmarking my code, will help me improve my coding skills?”.

This is an easy one. Benchmarking gives you knowledge, and knowledge is power. The better understanding of your code, the better code you’ll start programming. Is that simple!.

Benchmarking gives you knowledge, and knowledge is power.

Benchmarking gives you a simple view of how your code is performing. This view focuses on three main areas: elapsed timememory, and iterations per second.

  1. Elapsed time: is the amount of time your code takes to finish a task.
  2. Memory: is the allocated space in your drive that your code occupies to solve the task.
  3. Iterations per second: is the total number of repetitions your code can do the same task, over and over, in a second.

Ok, at this point I assume you have understood the basics of benchmarking. Hopefully, you are also saying to yourself - “Yeah, this is what I was missing, this will help me become a better programmer!”.

If you don’t think that way yet; I hope knowing how it works will do.

Now, how do we apply this?

Ruby makes it easy for us, as it already has a “Benchmarking” class, but we’ll complement it with two other ruby gems.

  1. Benchmarking-ips
  2. Benchmarking-memory

Before describing the steps to benchmark your code. I’m assuming you are using a linux/unix distribution, and have a working version of Ruby installed.

If you’re running some other OS like Windows, the next series of commands, probably, won’t work. But the benchmarking steps are going to be identical.

Let’s begin!.

a. The first step is to install this rubygems in your system; for that, you can use this command:

gem install benchmark-ips benchmark-memory

b. The second **step **is creating a ruby file, I’m going to call it “benchmarking_ruby.rb”, but you can name it in any way you want:

touch benchmarking_ruby.rb

b.1 Open the file with your preferred text editor. This file will contain three main sections:

- Section 1: Here we use require to include and grant our file access to the gems recently installed and the Ruby Benchmark Class. Doing so, will allow us to use the benchmark methods to measure our code performance.

require 'benchmark'
require 'benchmark/memory'
require 'benchmark/ips'

Section 2: We write the code that we want to benchmark.

def method_1(params)
    ## some code
end

def method_2(params)
    ## some code
end

Note:_ Benchmark works if you want to measure a single method but it’s best if you test at least two or more. Don’t worry too much about this right now, later on, I’ll show you an example and you’ll understand what I meant._

- Section 3: We set up the test.

This part is tricky but not complicated. Look at the image below and try to identify what’s happening.

def benchmark(params)
    Benchmark.bmbm do |x|
        x.report ("Method One") { method_1(params) }
        x.report ("Method Two") { method_2(params) }
    end

    Benchmark.memory do |x|
        x.report ("Method One") { method_1(params) }
        x.report ("Method Two") { method_2(params) }
        x.compare!
    end

    Benchmark.ips do |x|
        x.report ("Method One") { method_1(params) }
        x.report ("Method Two") { method_2(params) }
        x.compare!
    end
end

benchmark(params)

We are declaring a method called “benchmark” that accepts some parameters (or arguments).

def benchmark(params)
    ...
end

#ruby #benchmark #learn-to-code-ruby #rubygems #code-performance #how-to-benchmark-ruby #benchmark-ips #benchmark-memory

How Benchmarking Your Code Will Improve Your Ruby Skills
1.25 GEEK