I have a class that takes an array of integers and produces a multiplication table.
class MultiplicationTable
def initialize(num_ary)
@num_ary = num_ary.to_a
end
def generate
a1 = @num_ary.clone
a2 = @num_ary.clone
rows = []
rows << a1
a2.shift
a2.each do |x|
cols = [x]
a1[1..-1].each do |y|
cols << (x * y)
end
rows << cols
end
rows
end
end
So this:
MultiplicationTable.new(1.upto(5)).generate
produces this output:
[
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
[4, 8, 12, 16, 20],
[5, 10, 15, 20, 25]
]
Here are my results from benchmarking:
user system total real
100 entry table 0.000000 0.000000 0.000000 ( 0.001295)
1,000 entry table 0.120000 0.010000 0.130000 ( 0.132325)
10,000 entry table 15.290000 0.380000 15.670000 ( 15.747799)
Is there anything I can do to speedup/improve up this code?