Skip to content

Instantly share code, notes, and snippets.

@larryfox
Last active August 29, 2015 14:03
Show Gist options
  • Save larryfox/b999ef8484e5a2e61024 to your computer and use it in GitHub Desktop.
Save larryfox/b999ef8484e5a2e61024 to your computer and use it in GitHub Desktop.
require 'benchmark/ips'
require 'faker'
def extract_tld(email)
email.split('.'.freeze).last
end
emails = [].fill(0..1000) { Faker::Internet.email }
Benchmark.ips do |x|
x.report("sort!") do
emails.dup.sort { |a, b| extract_tld(a) <=> extract_tld(b) }
end
x.report("map!.sort!.map!") do
emails.dup
.map { |x| [extract_tld(x), x] }
.sort
.map { |x| x[1] }
end
x.report("sort_by!") do
emails.dup.sort_by { |x| extract_tld(x) }
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment