Profiling first, optimizing second
It's better to never guess where the slow parts of your library are, since you will often be incorrect. There is one way to know where the slow parts of your library are, and that is to profile your library. There are a couple of good options for profiling libraries in Ruby, ruby-prof
and stackprof
. There are other profilers for Ruby, such as rack-mini-profiler
and rbspy
, but they mostly focus on profiling production applications and not libraries, so we won't discuss them further. However, you may want to remember them if you need to profile a production application.
ruby-prof
is one of the oldest profiling libraries for Ruby, and still one of the best. It is a tracing profiler, meaning that it keeps track of every single method call Ruby is making, so it generally results in the most accurate profiling. However, because of this, it's the slowest profiler, about two to three times slower than running standard Ruby. This means it...