fork download
  1. require 'benchmark'
  2.  
  3. h = [{number: 1},{number: 2}, {number: 3}, {number: 4}]
  4. a = [3,4,1,2]
  5. index_table = Hash[a.map.with_index{|v,i|[v,i]}]
  6. n=10_000
  7.  
  8. Benchmark.bm(10) do |algo|
  9. algo.report("index"){n.times{ h.sort_by{|el| a.index(el[:number])}}}
  10. algo.report("index_table"){n.times{ h.sort_by{|el| index_table[el]}}}
  11. end
Success #stdin #stdout 0.16s 7688KB
stdin
Standard input is empty
stdout
                 user     system      total        real
index        0.040000   0.000000   0.040000 (  0.048163)
index_table  0.100000   0.000000   0.100000 (  0.096562)