public
Last active

  • Download Gist
sortbench.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
require 'benchmark'
include Benchmark
 
class Weeble
attr_reader :name, :age, :salary, :language
def initialize(name, age, salary, language)
@name, @age, @salary, @language = name, age, salary, language
end
 
def sorter
[@age || 0, @salary || 0, @language || "", @name || ""]
end
end
 
weebles = [
Weeble.new("Aaron", 24, 60_000, "perl"),
Weeble.new("Bob", 24, 60_000, "python"),
Weeble.new("Carl", 24, 80_000, "ruby"),
Weeble.new("David", 24, 80_000, "ruby"),
Weeble.new("Edward", 24, 90_000, "ruby"),
Weeble.new("Frank", 33, 60_000, "perl"),
Weeble.new("Greg", 33, 60_000, "python"),
Weeble.new("Hecor", 33, 80_000, "ruby"),
Weeble.new("Ivan", 33, 80_000, "ruby"),
Weeble.new("Josh", 33, 90_000, "ruby"),
Weeble.new("Kevin", 24, 60_000, "perl"),
Weeble.new("Lewis", 24, 60_000, "python"),
Weeble.new("Mike", 24, 80_000, "ruby"),
Weeble.new("Noah", 24, 80_000, "ruby"),
Weeble.new("Oscar", 24, 90_000, "ruby"),
Weeble.new("Paul", 33, 60_000, "perl"),
Weeble.new("Quentin", 33, 60_000, "python"),
Weeble.new("Ryan", 33, 80_000, "ruby"),
Weeble.new("Steve", 33, 80_000, "ruby"),
Weeble.new("Titus", 33, 90_000, "ruby"),
Weeble.new("Aaron", 24, 42_000, "perl"),
Weeble.new("Bob", 24, 42_000, "python"),
Weeble.new("Carl", 24, 42_000, "ruby"),
Weeble.new("David", 24, 42_000, "ruby"),
Weeble.new("Edward", 24, 42_000, "ruby"),
Weeble.new("Frank", 33, 42_000, "perl"),
Weeble.new("Greg", 33, 42_000, "python"),
Weeble.new("Hecor", 33, 42_000, "ruby"),
Weeble.new("Ivan", 33, 42_000, "ruby"),
Weeble.new("Josh", 33, 42_000, "ruby"),
Weeble.new("Kevin", 24, 42_000, "perl"),
Weeble.new("Lewis", 24, 42_000, "python"),
Weeble.new("Mike", 24, 42_000, "ruby"),
Weeble.new("Noah", 24, 42_000, "ruby"),
Weeble.new("Oscar", 24, 42_000, "ruby"),
Weeble.new("Paul", 33, 42_000, "perl"),
Weeble.new("Quentin", 33, 42_000, "python"),
Weeble.new("Ryan", 33, 42_000, "ruby"),
Weeble.new("Steve", 33, 42_000, "ruby"),
Weeble.new("Titus", 33, 42_000, "ruby"),
Weeble.new("Aaron", 27, 60_000, "perl"),
Weeble.new("Bob", 27, 60_000, "python"),
Weeble.new("Carl", 27, 80_000, "ruby"),
Weeble.new("David", 27, 80_000, "ruby"),
Weeble.new("Edward", 27, 90_000, "ruby"),
Weeble.new("Frank", 27, 60_000, "perl"),
Weeble.new("Greg", 21, 60_000, "python"),
Weeble.new("Hecor", 21, 80_000, "ruby"),
Weeble.new("Ivan", 21, 80_000, "ruby"),
Weeble.new("Josh", 21, 90_000, "ruby"),
Weeble.new("Kevin", 21, 60_000, "perl"),
Weeble.new("Lewis", 21, 60_000, "python"),
Weeble.new("Mike", 21, 80_000, "ruby"),
Weeble.new("Noah", 21, 80_000, "ruby"),
Weeble.new("Oscar", 27, 90_000, "ruby"),
Weeble.new("Paul", 27, 60_000, "perl"),
Weeble.new("Quentin", 27, 60_000, "python"),
Weeble.new("Ryan", 27, 80_000, "ruby"),
Weeble.new("Steve", 27, 80_000, "ruby"),
Weeble.new("Titus", 27, 90_000, "ruby")
]
 
sort_block = Proc.new do |a,b|
if 0 != (comp = ((a.age || 0) <=> (b.age || 0)))
comp
elsif 0 != (comp = ((a.salary || 0) <=> (b.salary || 0)))
comp
elsif 0 != (comp = ((a.language || "") <=> (b.language || "")))
comp
else
((a.name || "") <=> (b.name || ""))
end
end
 
bm(16) do |t|
t.report("sort") { 10_000.times { weebles.sort { |a,b| a.sorter <=> b.sorter } } }
t.report("sort_by") { 10_000.times { weebles.sort_by { |w| w.sorter } } }
t.report("sort block") { 10_000.times { weebles.sort(&sort_block) } }
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.