O'Reilly logo

Ruby Performance Optimization by Alexander Dymo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Make Internals Behave Predictably

Two things in our applications can randomly slow it down: GC and system calls. (I/O is the most common case of a system call.)

I can imaging you sighing, “Oh no, not GC again.” But in this case it’s easy to neutralize its effect on measurements, in one of two ways.

First, it’s sometimes OK to disable GC completely before measurement. If raw performance of our code is what we’re interested in, then it’s absolutely fine to do that.

If we want our measurement to be closer to the real world, we can try to make GC as predictable as possible by forcing it before executing the code, like this:

 
GC.start
 
measurement = Benchmark.realtime ​do
 
...
 
end

This way our code will trigger GC in the same manner ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required