public
Last active

Sorting arrays of related objects

  • Download Gist
coarrays.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
require 'benchmark'
 
N = 100_000
 
[3, 5, 10].each do |array_size|
 
terms = []
arr2 = []
array_size.times do |n|
terms << {:id => n, :name => "term#{n}", :version => 1}
arr2 << {:term_id => n, :name => "assoc#{n}"}
end
 
# Randomize them
terms.sort_by! { rand }
arr2.sort_by! { rand }
 
puts "Benchmark for arrays with #{array_size} items:"
puts "terms = #{terms.inspect}"
puts "arr2 = #{arr2.inspect}"
Benchmark.bm(10) do |x|
x.report('find_index:') do
N.times do
arr2.sort_by {|o| terms.find_index {|a| o[:term_id] == a[:id]}}
end
end
x.report('gen_lookup:') do
N.times do
lookup = {}
terms.each_with_index { |t, idx| lookup[t[:id]] = idx}
arr2.sort_by {|o| lookup[o[:term_id]]}
end
end
 
end
end
results.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Benchmark for arrays with 3 items:
terms = [{:id=>2, :name=>"term2", :version=>1}, {:id=>1, :name=>"term1", :version=>1}, {:id=>0, :name=>"term0", :version=>1}]
arr2 = [{:term_id=>2, :name=>"assoc2"}, {:term_id=>1, :name=>"assoc1"}, {:term_id=>0, :name=>"assoc0"}]
user system total real
find_index: 0.280000 0.000000 0.280000 ( 0.282137)
gen_lookup: 0.400000 0.000000 0.400000 ( 0.398467)
Benchmark for arrays with 5 items:
terms = [{:id=>3, :name=>"term3", :version=>1}, {:id=>4, :name=>"term4", :version=>1}, {:id=>0, :name=>"term0", :version=>1}, {:id=>1, :name=>"term1", :version=>1}, {:id=>2, :name=>"term2", :version=>1}]
arr2 = [{:term_id=>2, :name=>"assoc2"}, {:term_id=>1, :name=>"assoc1"}, {:term_id=>0, :name=>"assoc0"}, {:term_id=>3, :name=>"assoc3"}, {:term_id=>4, :name=>"assoc4"}]
user system total real
find_index: 0.470000 0.000000 0.470000 ( 0.471214)
gen_lookup: 0.500000 0.010000 0.510000 ( 0.510521)
Benchmark for arrays with 10 items:
terms = [{:id=>5, :name=>"term5", :version=>1}, {:id=>7, :name=>"term7", :version=>1}, {:id=>9, :name=>"term9", :version=>1}, {:id=>3, :name=>"term3", :version=>1}, {:id=>6, :name=>"term6", :version=>1}, {:id=>8, :name=>"term8", :version=>1}, {:id=>1, :name=>"term1", :version=>1}, {:id=>0, :name=>"term0", :version=>1}, {:id=>4, :name=>"term4", :version=>1}, {:id=>2, :name=>"term2", :version=>1}]
arr2 = [{:term_id=>9, :name=>"assoc9"}, {:term_id=>5, :name=>"assoc5"}, {:term_id=>1, :name=>"assoc1"}, {:term_id=>6, :name=>"assoc6"}, {:term_id=>4, :name=>"assoc4"}, {:term_id=>0, :name=>"assoc0"}, {:term_id=>8, :name=>"assoc8"}, {:term_id=>3, :name=>"assoc3"}, {:term_id=>7, :name=>"assoc7"}, {:term_id=>2, :name=>"assoc2"}]
user system total real
find_index: 1.240000 0.000000 1.240000 ( 1.240375)
gen_lookup: 0.870000 0.010000 0.880000 ( 0.880777)
Benchmark for arrays with 30 items:
terms = [{:id=>6, :name=>"term6", :version=>1}, {:id=>20, :name=>"term20", :version=>1}, {:id=>19, :name=>"term19", :version=>1}, {:id=>29, :name=>"term29", :version=>1}, {:id=>24, :name=>"term24", :version=>1}, {:id=>4, :name=>"term4", :version=>1}, {:id=>5, :name=>"term5", :version=>1}, {:id=>3, :name=>"term3", :version=>1}, {:id=>11, :name=>"term11", :version=>1}, {:id=>18, :name=>"term18", :version=>1}, {:id=>0, :name=>"term0", :version=>1}, {:id=>28, :name=>"term28", :version=>1}, {:id=>23, :name=>"term23", :version=>1}, {:id=>27, :name=>"term27", :version=>1}, {:id=>1, :name=>"term1", :version=>1}, {:id=>22, :name=>"term22", :version=>1}, {:id=>9, :name=>"term9", :version=>1}, {:id=>8, :name=>"term8", :version=>1}, {:id=>14, :name=>"term14", :version=>1}, {:id=>17, :name=>"term17", :version=>1}, {:id=>10, :name=>"term10", :version=>1}, {:id=>26, :name=>"term26", :version=>1}, {:id=>21, :name=>"term21", :version=>1}, {:id=>2, :name=>"term2", :version=>1}, {:id=>25, :name=>"term25", :version=>1}, {:id=>7, :name=>"term7", :version=>1}, {:id=>13, :name=>"term13", :version=>1}, {:id=>12, :name=>"term12", :version=>1}, {:id=>16, :name=>"term16", :version=>1}, {:id=>15, :name=>"term15", :version=>1}]
arr2 = [{:term_id=>11, :name=>"assoc11"}, {:term_id=>15, :name=>"assoc15"}, {:term_id=>19, :name=>"assoc19"}, {:term_id=>23, :name=>"assoc23"}, {:term_id=>5, :name=>"assoc5"}, {:term_id=>18, :name=>"assoc18"}, {:term_id=>21, :name=>"assoc21"}, {:term_id=>0, :name=>"assoc0"}, {:term_id=>12, :name=>"assoc12"}, {:term_id=>27, :name=>"assoc27"}, {:term_id=>29, :name=>"assoc29"}, {:term_id=>1, :name=>"assoc1"}, {:term_id=>3, :name=>"assoc3"}, {:term_id=>26, :name=>"assoc26"}, {:term_id=>2, :name=>"assoc2"}, {:term_id=>13, :name=>"assoc13"}, {:term_id=>20, :name=>"assoc20"}, {:term_id=>25, :name=>"assoc25"}, {:term_id=>22, :name=>"assoc22"}, {:term_id=>9, :name=>"assoc9"}, {:term_id=>24, :name=>"assoc24"}, {:term_id=>7, :name=>"assoc7"}, {:term_id=>10, :name=>"assoc10"}, {:term_id=>28, :name=>"assoc28"}, {:term_id=>16, :name=>"assoc16"}, {:term_id=>17, :name=>"assoc17"}, {:term_id=>4, :name=>"assoc4"}, {:term_id=>6, :name=>"assoc6"}, {:term_id=>14, :name=>"assoc14"}, {:term_id=>8, :name=>"assoc8"}]
user system total real
find_index: 8.440000 0.010000 8.450000 ( 8.461052)
gen_lookup: 2.500000 0.040000 2.540000 ( 2.540562)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.