In Praise of Fruity

Another tech review, this time a Ruby library. Fruity is
a library for performance testing. You have probably heard of its better-known
cousin, Benchmark.

When I’m interested in benchmarking, it’s usually for small methods. I’ll think
of a different way to approach a problem, and then the thought will occur to me,
“But is it performant?” Fruity is ideal for these kind of situations. But why
might I want to import a library when Benchmark is built in?

Let’s take a recent example. I was working through the Minesweeper exercise on
Exercism.io. I was thinking if there was a better way to
make sure my Minesweeper grid didn’t have any illegal characters in it. The
first way was to convert an array of characters into a string, then to use
count to make sure the count of all unwanted characters was zero.

After I finished the exercise, I thought about another way. The Array class
has an intersection operator &. If I used that, I wouldn’t have to convert the
array to a string and maybe save some cycles.

When I’m wanting a Ruby sandbox, I turn to Pry. I don’t
use most of Pry’s features, but I like it over IRB for its better syntax
highlighting.

Note that this output is a little cryptic. It does tell me that the first way
is faster, but I need to do a little work. I had to think about the number
of iterations I wanted for the test. And, I had to do some mental math to
figure out the real answer I wanted, which is “which one is faster and by
how much?”

I didn’t have to specify the number of iterations. Fruity came up with an
intelligent guess.

Fruity reported its results in relative terms

A simple test like this highlights a different in approach between Fruity and
other benchmarking tools. For more on that, take a look at the project’s README
on Github. In
short, Fruity seeks to eliminate noise by not reporting insignificant
differences.

Fruity can do more than just anonymous methods, as I’ve shown here. It can
compare methods on a class or another kind of callable. It can also do
comparisons with parameters.

In cases like mine, clarity trumps precision. It’s the difference between asking
LaForge and Data a question. I’d prefer some synthesis over a data dump (no pun
intended). Next time you’re wondering about performance in Ruby, give Fruity a
try.