Skip to content

Commit

Permalink
Add Enumerable#sort_by.first vs Enumerable#min_by
Browse files Browse the repository at this point in the history
  • Loading branch information
parkerfinch committed Apr 8, 2018
1 parent c2b3ce2 commit 81a47dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,30 @@ Comparison:
Array#reverse.each: 190729.1 i/s - 1.13x slower
```

##### `Enumerable#sort_by.first` vs `Enumerable#min_by` [code](code/enumerable/sort_by-first-vs-min_by.rb)
`Enumerable#sort_by` performs a sort of the enumerable and allocates a
new array the size of the enumerable. `Enumerable#min_by` doesn't
perform a sort or allocate an array the size of the enumerable.
Similar comparisons hold for `Enumerable#sort_by.last` vs `
`Enumerable#max_by`, `Enumerable#sort.first` vs `Enumerable#min`, and
`Enumerable#sort.last` vs `Enumerable#max`.

```
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
Warming up --------------------------------------
Enumerable#min_by 15.170k i/100ms
Enumerable#sort_by...first
10.413k i/100ms
Calculating -------------------------------------
Enumerable#min_by 157.877k (± 0.9%) i/s - 804.010k in 5.093048s
Enumerable#sort_by...first
106.831k (± 1.3%) i/s - 541.476k in 5.069403s
Comparison:
Enumerable#min_by: 157877.0 i/s
Enumerable#sort_by...first: 106831.1 i/s - 1.48x slower
```

##### `Enumerable#detect` vs `Enumerable#select.first` [code](code/enumerable/select-first-vs-detect.rb)

```
Expand Down
17 changes: 17 additions & 0 deletions code/enumerable/sort_by-first-vs-min_by.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'benchmark/ips'

ARRAY = [*1..100]

def fast
ARRAY.min_by { |x| x.succ }
end

def slow
ARRAY.sort_by { |x| x.succ }.first
end

Benchmark.ips do |x|
x.report('Enumerable#min_by') { fast }
x.report('Enumerable#sort_by...first') { slow }
x.compare!
end

0 comments on commit 81a47dc

Please sign in to comment.