public
Created

Comparing how to do a reverse sort

  • Download Gist
compare_reverse_sort.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
require 'fruity'
 
a = (1..100).to_a.shuffle
compare do
sort { a.sort{|x, y| y <=> x} }
sort_by { a.sort_by{|x| -x} }
reverse { a.sort.reverse }
reverse! { a.sort.reverse! }
end
 
# reverse! is faster than reverse by 10% ± 1.0%
# reverse is faster than sort_by by 6x ± 0.1
# sort_by is faster than sort by 60% ± 1.0%

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.