Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
27 lines (20 sloc) 474 Bytes
require "benchmark/ips"
User = Struct.new(:name)
ARRAY = Array.new(100) do
User.new(sprintf "%010d", rand(1_000_000_000))
end
def fastest
ARRAY.sort_by(&:name)
end
def faster
ARRAY.sort_by { |element| element.name }
end
def slow
ARRAY.sort { |a, b| a.name <=> b.name }
end
Benchmark.ips do |x|
x.report('Enumerable#sort_by (Symbol#to_proc)') { fastest }
x.report('Enumerable#sort_by') { faster }
x.report('Enumerable#sort') { slow }
x.compare!
end
You can’t perform that action at this time.