Skip to content

Instantly share code, notes, and snippets.

@DimaD
Created March 1, 2010 16:24
Show Gist options
  • Save DimaD/318505 to your computer and use it in GitHub Desktop.
Save DimaD/318505 to your computer and use it in GitHub Desktop.
# Ruby 1.8.7
# user system total real
# partition 0.180000 0.000000 0.180000 ( 0.185015)
# sort_by 0.160000 0.010000 0.170000 ( 0.160012)
# Ruby 1.9
# user system total real
# partition 0.090000 0.000000 0.090000 ( 0.092589)
# sort_by 0.160000 0.010000 0.170000 ( 0.166785)
require 'benchmark'
number_of_people = 50000
Person = Struct.new(:emails, :publications)
people = []
number_of_people.times do |i|
people << Person.new(rand(5), rand(10))
end
Benchmark.bm(10) do |benchmark|
benchmark.report('partition') do
people_with_emails, people_without_emails = people.partition { |person| person.emails.size > 0 }
result = (people_with_emails + people_without_emails).sort { |a, b| a.publications <=> b.publications }
end
benchmark.report('sort_by') do
people.sort_by { |person| [person.emails.size > 0 ? 1 : 0, person.publications] }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment