Speedy array operations with Ruby 2.4
Ruby 2.4 has optimized Array#min
and Array#max
methods. Here are some benchmarks.
Ruby 2.4-preview1 was released today.
Up until 2.3
, ruby’s Array
class took methods from Enumerable
mixin which included the #min
and #max
.
Now in 2.4
, array has its own min
and max
methods which are way faster than the old ones.
These methods now completely skip the call to Enumerable#each
.
Ruby maintainers claim it will benificial in big data operations where min/max
are the most basic and frequent operations.
Simple script for benchmarking: (using shuffle to remove the chance of algorithm being biased towards sorted arrays)
Running these benchmarks on my macbook air. First on ruby 2.3
:
And now on ruby 2.4.0preview1
:
This is a whopping 20x performace gain!
References:
- Ruby
2.4-preview1
release announcement: https://www.ruby-lang.org/en/news/2016/06/20/ruby-2-4-0-preview1-released/ - Rubylang issue tracker: https://bugs.ruby-lang.org/issues/12172