more faster
require 'benchmark'
strings = []
10000.times do
string = Array.new(rand(500..1000)) { rand(10).to_s }.join('')
strings << string
end
def solution1(array)
array.each do |s|
s.chars.sum(&:to_i)
end
end
def solution2(array)
array.each do |s|
s.sum - s.size * 48
end
end
Benchmark.bmbm do |x|
x.report { solution1(strings) }
x.report { solution2(strings) }
end
result
Rehearsal ------------------------------------
1.032923 0.004906 1.037829 ( 1.044178)
0.009852 0.000544 0.010396 ( 0.010945)
--------------------------- total: 1.048225sec
user system total real
1.032176 0.006350 1.038526 ( 1.045689)
0.007727 0.000118 0.007845 ( 0.007965)