reverse.detect vs select { ... }.last
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- performance
Research direction
Start by running the supplied reverse.rb benchmark with benchmark-ips and compare the three shown approaches. The issue does not name a repository file or specify the proposed third approach; done would require identifying and documenting a concrete alternative with benchmark results.
Written by the indexing model from the issue text.
Description
Third approach?
require 'benchmark/ips'
ARRAY = [*1..100]
def faster
ARRAY.reverse_each { |x| break x if (x % 10).zero? }
end
def fast
ARRAY.reverse.detect { |x| (x % 10).zero? }
end
def slow
ARRAY.select { |x| (x % 10).zero? }.last
end
Benchmark.ips do |x|
x.report('Enumerable#reverse_each + break') { faster }
x.report('Enumerable#reverse.detect') { fast }
x.report('Enumerable#select.last') { slow }
x.compare!
end
% ruby reverse.rb
Calculating -------------------------------------
Enumerable#reverse_each + break
115.335k i/100ms
Enumerable#reverse.detect
72.531k i/100ms
Enumerable#select.last
11.243k i/100ms
-------------------------------------------------
Enumerable#reverse_each + break
3.311M (± 7.2%) i/s - 16.493M
Enumerable#reverse.detect
1.255M (± 9.3%) i/s - 6.238M
Enumerable#select.last
129.592k (± 3.4%) i/s - 652.094k
Comparison:
Enumerable#reverse_each + break: 3310776.9 i/s
Enumerable#reverse.detect: 1255082.9 i/s - 2.64x slower
Enumerable#select.last: 129592.0 i/s - 25.55x slower
- Dominant language
- Ruby
- Stars
- 5.7k
- Forks
- 370
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastruby/fast-ruby
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
All issues in fastruby/fast-ruby
Similar issues
-
SyncEm always forwards a dummy block, so wrapped methods lose their no-block/Enumerator behavior Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
dependabot
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
githubnext/gh-aw-cao#13349 ·
-
user-reported
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kong/developer.konghq.com#7316 ·