Skip to content

Instantly share code, notes, and snippets.

@rab
Created August 7, 2015 18:37
Show Gist options
  • Save rab/4db0fc1795c9db742dab to your computer and use it in GitHub Desktop.
Save rab/4db0fc1795c9db742dab to your computer and use it in GitHub Desktop.
Array to Hash of counts
( time ruby ./count_bm.rb ; \
time ruby ./count_bm.rb 10000 10000 ; \
time ruby ./count_bm.rb 1000 100000 ; \
time ruby ./count_bm.rb 100000 10 \
) | tee count_bm.txt
require 'benchmark'
n = (ARGV.shift || 10_000).to_i
a = (ARGV.shift || 1_000).to_i
arr = Array.new(a) {|i| ('a'.ord + rand(26)).chr }
puts
puts "Iterations: %6d"%[n]
puts "Array size: %6d"%[a]
Benchmark.bm(32) do |x|
x.report("uniq.map.to_h :") {
n.times {
arr.uniq.map { |x| [x, arr.count(x)] }.to_h # 2 18.2%
}
}
x.report("Hash;each :") {
n.times {
h = Hash.new(0); arr.each { |l| h[l] += 1 }; h # 3 27.3%
}
}
x.report("reduce;to_i :") {
n.times {
arr.reduce({}) { |m, a| m[a] = m[a].to_i + 1; m } # 0 0%
}
}
x.report("inject :") {
n.times {
arr.inject(Hash.new(0)) { |h, i| h[i] += 1; h } # 1 9.1%
}
}
x.report("sort.chunk.map.to_h :") {
n.times {
arr.sort.chunk { |ex| ex }.map { |k, v| [k, v.length] }.to_h # 0 0%
}
}
x.report("reduce.||0 :") {
n.times {
arr.reduce({}) { |ret, val| ret[val] = (ret[val] || 0) + 1; ret } # 2 18.2%
}
}
x.report("each_with_object(Hash.new(0)) :") {
n.times {
arr.each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 } # 2 18.2%
}
}
x.report("each_with_object({}).||=0 :") {
n.times {
arr.each_with_object({}) { |item, memo| memo[item] ||= 0; memo[item] += 1 } # 1 9.1%
}
}
x.report("map.inject.merge :") {
n.times {
arr.map { |x| { x => 1 } }.inject { |a, b| a.merge(b) { |k, x, y| x + y } } # 0 0%
}
}
x.report("group_by{.length}.to_h :") {
n.times {
arr.group_by { |x| x }.map { |element, matches| [ element, matches.length ] }.to_h # 0 0%
}
}
x.report("Hash[group_by{.size}] :") {
n.times {
Hash[arr.group_by(&:itself).map {|k,v| [k, v.size] }] # Must also upgrade to Ruby 2.2 or Rails 4 0 0%
}
}
x.report("sort.chunk.map.to_h:") {
n.times {
arr.sort.chunk(&:itself).map {|v, vs| [v, vs.count]}.to_h # Must also upgrade to Ruby 2.2 or Rails 4
}
}
end
Iterations: 10000
Array size: 1000
user system total real
uniq.map.to_h : 13.790000 0.060000 13.850000 ( 13.939324)
Hash;each : 2.150000 0.010000 2.160000 ( 2.181709)
reduce;to_i : 2.680000 0.010000 2.690000 ( 2.714307)
inject : 2.440000 0.020000 2.460000 ( 2.468704)
sort.chunk.map.to_h : 3.760000 0.020000 3.780000 ( 3.811044)
reduce.||0 : 2.440000 0.010000 2.450000 ( 2.474357)
each_with_object(Hash.new(0)) : 2.420000 0.010000 2.430000 ( 2.450474)
each_with_object({}).||=0 : 3.270000 0.020000 3.290000 ( 3.306296)
map.inject.merge : 129.510000 2.310000 131.820000 (132.544855)
group_by{.length}.to_h : 2.050000 0.020000 2.070000 ( 2.076691)
Hash[group_by{.size}] : 2.140000 0.020000 2.160000 ( 2.223103)
sort.chunk.map.to_h: 3.780000 0.030000 3.810000 ( 3.902351)
Iterations: 10000
Array size: 10000
user system total real
uniq.map.to_h : 133.790000 0.370000 134.160000 (134.710460)
Hash;each : 19.960000 0.070000 20.030000 ( 20.131524)
reduce;to_i : 25.350000 0.090000 25.440000 ( 25.566145)
inject : 22.680000 0.080000 22.760000 ( 22.855127)
sort.chunk.map.to_h : 34.250000 0.170000 34.420000 ( 34.568563)
reduce.||0 : 23.210000 0.110000 23.320000 ( 23.475830)
each_with_object(Hash.new(0)) : 22.980000 0.100000 23.080000 ( 23.222549)
each_with_object({}).||=0 : 30.570000 0.080000 30.650000 ( 30.763687)
map.inject.merge : 1369.400000 47.150000 1416.550000 (1435.929708)
group_by{.length}.to_h : 18.340000 0.260000 18.600000 ( 19.183591)
Hash[group_by{.size}] : 17.350000 0.190000 17.540000 ( 17.943907)
sort.chunk.map.to_h: 38.670000 0.460000 39.130000 ( 40.816240)
Iterations: 1000
Array size: 100000
user system total real
uniq.map.to_h : 159.720000 1.240000 160.960000 (164.781362)
Hash;each : 21.980000 0.120000 22.100000 ( 22.287815)
reduce;to_i : 27.610000 0.140000 27.750000 ( 28.022940)
inject : 24.880000 0.140000 25.020000 ( 25.233635)
sort.chunk.map.to_h : 57.780000 0.650000 58.430000 ( 60.216062)
reduce.||0 : 26.820000 0.160000 26.980000 ( 27.694940)
each_with_object(Hash.new(0)) : 24.960000 0.140000 25.100000 ( 25.311553)
each_with_object({}).||=0 : 33.820000 0.200000 34.020000 ( 34.372526)
map.inject.merge : 1388.700000 28.650000 1417.350000 (1428.944426)
group_by{.length}.to_h : 17.340000 0.290000 17.630000 ( 17.747058)
Hash[group_by{.size}] : 17.680000 0.250000 17.930000 ( 18.074335)
sort.chunk.map.to_h: 54.970000 0.310000 55.280000 ( 55.678994)
Iterations: 100000
Array size: 10
user system total real
uniq.map.to_h : 1.330000 0.010000 1.340000 ( 1.338240)
Hash;each : 0.500000 0.000000 0.500000 ( 0.514847)
reduce;to_i : 0.600000 0.010000 0.610000 ( 0.608918)
inject : 0.580000 0.010000 0.590000 ( 0.600932)
sort.chunk.map.to_h : 1.640000 0.000000 1.640000 ( 1.666477)
reduce.||0 : 0.550000 0.010000 0.560000 ( 0.560651)
each_with_object(Hash.new(0)) : 0.570000 0.010000 0.580000 ( 0.593885)
each_with_object({}).||=0 : 0.680000 0.000000 0.680000 ( 0.692728)
map.inject.merge : 2.640000 0.060000 2.700000 ( 2.710757)
group_by{.length}.to_h : 1.070000 0.000000 1.070000 ( 1.079730)
Hash[group_by{.size}] : 1.080000 0.010000 1.090000 ( 1.096178)
sort.chunk.map.to_h: 1.620000 0.010000 1.630000 ( 1.631976)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment