Skip to content

Instantly share code, notes, and snippets.

@holderbaum
Created October 3, 2012 09:23
Show Gist options
  • Save holderbaum/3826016 to your computer and use it in GitHub Desktop.
Save holderbaum/3826016 to your computer and use it in GitHub Desktop.
Test for efficient sorting of an array of hashes
require 'benchmark'
class EntryHash < Hash
def <=>(other)
self[:value] <=> (other && other[:value] || 0)
end
end
10.times do |i|
num = 100_000 * (i+1)
plain_array1 = (1..num).map do |i|
{ :id => i, :value => rand(50) }
end
plain_array2 = plain_array1.dup
object_array1 = plain_array1.map do |old|
e = EntryHash.new
e[:id] = old[:id]
e[:value] = old[:value]
e
end
p :with => num
Benchmark.bm do |x|
x.report("inline block with sort! ") do
plain_array1.sort! do |a, b|
(a && a[:value] || 0) <=> (b && b[:value] || 0)
end
end
x.report("inline block with sort_by! ") do
plain_array2.sort_by! do |a, b|
(a && a[:value] || 0) <=> (b && b[:value] || 0)
end
end
x.report("entry method with sort! ") do
object_array1.sort!
end
end
end
## Result:
#
# {:with=>100000}
# user system total real
# inline block with sort! 0.200000 0.000000 0.200000 ( 0.197349)
# inline block with sort_by! 0.030000 0.000000 0.030000 ( 0.061152)
# entry method with sort! 0.220000 0.000000 0.220000 ( 0.221882)
# {:with=>200000}
# user system total real
# inline block with sort! 0.410000 0.000000 0.410000 ( 0.423499)
# inline block with sort_by! 0.130000 0.000000 0.130000 ( 0.123960)
# entry method with sort! 0.470000 0.000000 0.470000 ( 0.474395)
# {:with=>300000}
# user system total real
# inline block with sort! 0.620000 0.010000 0.630000 ( 0.626362)
# inline block with sort_by! 0.180000 0.010000 0.190000 ( 0.275429)
# entry method with sort! 0.680000 0.000000 0.680000 ( 0.684978)
# {:with=>400000}
# user system total real
# inline block with sort! 0.820000 0.000000 0.820000 ( 0.832784)
# inline block with sort_by! 0.240000 0.010000 0.250000 ( 0.366189)
# entry method with sort! 0.920000 0.000000 0.920000 ( 0.923894)
# {:with=>500000}
# user system total real
# inline block with sort! 1.110000 0.000000 1.110000 ( 1.109297)
# inline block with sort_by! 0.160000 0.000000 0.160000 ( 0.315537)
# entry method with sort! 1.200000 0.000000 1.200000 ( 1.206631)
# {:with=>600000}
# user system total real
# inline block with sort! 1.330000 0.000000 1.330000 ( 1.337569)
# inline block with sort_by! 0.380000 0.000000 0.380000 ( 0.378962)
# entry method with sort! 1.460000 0.000000 1.460000 ( 1.463542)
# {:with=>700000}
# user system total real
# inline block with sort! 1.460000 0.000000 1.460000 ( 1.461316)
# inline block with sort_by! 0.430000 0.020000 0.450000 ( 0.444706)
# entry method with sort! 1.620000 0.000000 1.620000 ( 1.630328)
# {:with=>800000}
# user system total real
# inline block with sort! 1.790000 0.000000 1.790000 ( 1.789210)
# inline block with sort_by! 0.520000 0.000000 0.520000 ( 0.763954)
# entry method with sort! 1.960000 0.000000 1.960000 ( 1.971014)
# {:with=>900000}
# user system total real
# inline block with sort! 1.950000 0.010000 1.960000 ( 1.971329)
# inline block with sort_by! 0.580000 0.000000 0.580000 ( 0.855665)
# entry method with sort! 2.160000 0.000000 2.160000 ( 2.159218)
# {:with=>1000000}
# user system total real
# inline block with sort! 2.410000 0.010000 2.420000 ( 2.414475)
# inline block with sort_by! 0.660000 0.020000 0.680000 ( 0.992156)
# entry method with sort! 2.650000 0.000000 2.650000 ( 2.654844)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment