MikeMirzayanov/binary-heap-benchmark — pull request #10 by skanev (2013-12-20 09:35:31+00:00)
Title: Rewrote the Ruby code

I rewrote the Ruby code. It had two serious flaws.

First, it was in no way idiomatic. The standard ruby naming convention was not adhered to and it used a global variable, probably because the author was not aware of some specifics in the scoping in Ruby. I've also changed a bunch of things to be way more idiomatic, like constructing the initial array or verifying its size. It should work on both Ruby 1.9 and Ruby 2.0.

Second, I changed the way exchanging of the elements was done. It abuses the splat assignments. Writing `a, b = b, a` creates two arrays in Ruby. Most of the time in the algorithm was actually spent on those two lines.

With both changes, I dropped down the running time on my machine from `256.65s` to `49.84s`. That's five times faster.

That's also twice as fast than the Python implementation (even with the rewritten exchange).

On a related note, comparing integers in Ruby is extra slow, because you can redefine `Numeric#<` and it basically has to do method lookup (or at least cache the result and check if the cache is valid).