public
Created

  • Download Gist
array_sort_bench.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
require 'benchmark'
 
iterations = 10_000
Benchmark.bm(20) do |x|
arrays = [
[[], 'empty'],
 
[(1..10).to_a, '10, in order'],
[(1..10).to_a.reverse, '10, reversed'],
[10.times.inject([]){|a,_|a<<rand(10);a}, '10, random'],
 
[(1..100).to_a, '100, in order'],
[(1..100).to_a.reverse, '100, reversed'],
[100.times.inject([]){|a,_|a<<rand(100);a}, '100, random'],
 
[(1..1000).to_a, '1,000, in order'],
[(1..1000).to_a.reverse, '1,000, reversed'],
[1000.times.inject([]){|a,_|a<<rand(1000);a}, '1,000, random'],
 
[(1..10_000).to_a, '10,000, in order'],
[(1..10_000).to_a.reverse, '10,000, reversed'],
[10_000.times.inject([]){|a,_|a<<rand(10_000);a}, '10,000, random'],
]
 
arrays.each do |ary,msg|
x.report(msg) { for i in 1..iterations; ary.dup.sort; end }
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.