Is there an inbuilt method to sum all values in a hash and return the
result?
I.E something like puts hash[values].sum
If so could someone please point me at the correct section of the ruby docs?
Maybe write your own method because:
1. it may be quicker than searching for an "inbuilt" method;
2. it may run faster than using inject.
require "benchmark"
kt = 100_000
nn = 20
hash = Hash.new
(1 .. nn).each { |nv| hash[ nv.to_s ] = nv }
s1 = s2 = s3 = s4 = nil
class Hash
def mysum()
s = 0
each_value { |v| s += v }
s
end
end
sv = (nn * (nn + 1)) / 2
puts; puts "summing 1..#{nn} #=> #{sv}; kt=#{kt};"
Benchmark.bmbm do |bm|
bm.report("1"){kt.times{s1=hash.values.inject{ |a, b| a + b } }}
bm.report("2"){kt.times{s2=hash.inject(0){ |sum, tuple| sum += tuple[1] } }}
bm.report("3"){kt.times{s3=hash.inject(0){|a,(_,b)|a+b} }}
bm.report("4"){kt.times{s4=hash.mysum }}
end
puts "#{s1.inspect} #{s2.inspect} #{s3.inspect} #{s4.inspect}"
ruby 1.9.1p430 (2010-08-16 revision 28998) [i386-mingw32]
summing 1..20 #=> 210; kt=100000;
user system total real
1 3.057000 0.000000 3.057000 ( 3.057547)
2 4.587000 0.000000 4.587000 ( 4.630071)
3 4.524000 0.000000 4.524000 ( 4.525069)
4 1.653000 0.000000 1.653000 ( 1.650026)
210 210 210 210
summing 1..2000 #=> 2001000; kt=1000;
user system total real
1 2.340000 0.000000 2.340000 ( 2.335036)
2 4.711000 0.000000 4.711000 ( 4.705072)
3 4.617000 0.000000 4.617000 ( 4.635071)
4 1.529000 0.000000 1.529000 ( 1.530024)
2001000 2001000 2001000 2001000