To learn what makes Ruby code fast, we must understand what makes Ruby code slow.
If you’ve done any performance optimization in the past, you probably think you know what makes code slow. You may think that even if you haven’t done performance optimization. Let me see if I can guess what you think.
Your first guess is algorithmic complexity of the code: extra nested loops, computations, that sort of stuff. And what would you do to fix the algorithmic complexity? Well, you would profile the code, locate the slow section, identify the reason for the slowness, and rewrite the code to avoid the bottleneck. Rinse and repeat until fast.
Sounds like a good plan, right? However, it doesn’t always work for Ruby code. ...