Skip to content

Instantly share code, notes, and snippets.

@marcandre
Created May 18, 2013 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcandre/5604592 to your computer and use it in GitHub Desktop.
Save marcandre/5604592 to your computer and use it in GitHub Desktop.
Comparing how to do a reverse sort
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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment