Open nerdrew opened 8 years ago
This test is biased in favor of reverse each because it bails on the first element, 100. A fairer test would be to search for (x % 51).zero?, since we expect to traverse about half the elements on average.
Results with x % 51
Comparison:
Enumerable#reverse_each + break: 257525.8 i/s
Enumerable#reverse.detect: 189938.5 i/s - 1.36x slower
Enumerable#select.last: 129624.6 i/s - 1.99x slower
Wow. That sure seems to level the playing field.
I'd propose replacing reverse.detect
with reverse_each.detect
in the comparison, rather than adding it as a third option, for all the reasons listed in the reverse.each vs reverse_each section.
If there's support behind this, I'm happy to whip up a PR for it!
Yes, reverse_each.detect
and reverse_each.any?
are going to be more performant than select.last
or reverse.detect
.
This is even documented in another benchmark on the same page. But reference material by definition usually means you look at the one relevant entry.
It's worth an update!
Third approach?