Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Last active August 29, 2015 14:14
Show Gist options
  • Save copiousfreetime/fc518d4ee872b815ce1a to your computer and use it in GitHub Desktop.
Save copiousfreetime/fc518d4ee872b815ce1a to your computer and use it in GitHub Desktop.
sort_vs_sort_by.rb
jeremy@ample-3:~/tmp ruby-2.1.5@default % ruby sort_vs_sort_by.rb
Rehearsal ------------------------------------------------------------------------------
#sort array of hashes 0.760000 0.010000 0.770000 ( 0.759593)
#sort_by array of hashes 0.290000 0.000000 0.290000 ( 0.299255)
#sort array of strings 0.550000 0.000000 0.550000 ( 0.554952)
#sort_by array of strings 0.280000 0.000000 0.280000 ( 0.276396)
#sort direct array of strings 0.100000 0.010000 0.110000 ( 0.102575)
#sort_by array of strings(2) (no counting) 0.260000 0.000000 0.260000 ( 0.266884)
--------------------------------------------------------------------- total: 2.260000sec
user system total real
#sort array of hashes 0.720000 0.000000 0.720000 ( 0.727211)
#sort_by array of hashes 0.310000 0.000000 0.310000 ( 0.302691)
#sort array of strings 0.540000 0.000000 0.540000 ( 0.538158)
#sort_by array of strings 0.270000 0.000000 0.270000 ( 0.276805)
#sort direct array of strings 0.090000 0.000000 0.090000 ( 0.098608)
#sort_by array of strings(2) (no counting) 0.270000 0.000000 0.270000 ( 0.277430)
Block invocations counts
========================
#sort array of hashes : 9487000
#sort_by array of hashes : 1000000
#sort array of strings : 9487000
#sort_by array of strings : 1000000
j
#!/usr/bin/env ruby
#
# To help answer the question at:
#
# https://www.omniref.com/ruby/2.2.0/symbols/Enumerable/sort_by?oes=new_annotation#annotation=4074262A
#
#
require 'benchmark'
require 'securerandom'
iterations = (ARGV.shift || 500).to_i
strings = Array.new(1000) { SecureRandom.hex }
ary = strings.map { |n| { :foo => n } }
count_sort_array_of_hashes = 0
count_sort_by_array_of_hashes = 0
count_sort_array_of_strings = 0
count_sort_by_array_of_strings = 0
Benchmark.bmbm(40) do |x|
x.report("#sort array of hashes") do
iterations.times do
ary.sort {|a,b|
count_sort_array_of_hashes +=1
a[:foo] <=> b[:foo]
}
end
end
x.report("#sort_by array of hashes") do
iterations.times do
ary.sort_by { |a|
count_sort_by_array_of_hashes += 1
a[:foo]
}
end
end
x.report("#sort array of strings") do
iterations.times do
strings.sort { |a,b|
count_sort_array_of_strings +=1
a <=> b
}
end
end
x.report("#sort_by array of strings") do
iterations.times do
strings.sort_by { |a|
count_sort_by_array_of_strings += 1
a
}
end
end
x.report("#sort direct array of strings") do
iterations.times do
strings.dup.sort
end
end
x.report("#sort_by array of strings(2) (no counting)") do
iterations.times do
strings.dup.sort_by { |a| a }
end
end
end
puts
puts "Block invocations counts"
puts "========================"
puts
puts "#sort array of hashes : #{count_sort_array_of_hashes}"
puts "#sort_by array of hashes : #{count_sort_by_array_of_hashes}"
puts "#sort array of strings : #{count_sort_array_of_strings}"
puts "#sort_by array of strings : #{count_sort_by_array_of_strings}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment