Skip to content

Instantly share code, notes, and snippets.

@rmm5t
Created June 30, 2019 14:23
Show Gist options
  • Save rmm5t/63d661c67a181f7b1c5031e1451dbdde to your computer and use it in GitHub Desktop.
Save rmm5t/63d661c67a181f7b1c5031e1451dbdde to your computer and use it in GitHub Desktop.
Benchmark Array#product
require 'benchmark'
def test_nested_loop
1.upto(20) do |x|
1.upto(30) do |y|
x * y
end
end
end
def test_array_product
(1..20).to_a.product((1..30).to_a) do |x, y|
x * y
end
end
n = 10_000
Benchmark.bmbm(15) do |x|
x.report("nested loop") { n.times { test_nested_loop } }
x.report("Array#product") { n.times { test_array_product } }
end
# >>
# >> user system total real
# >> nested loop 0.209984 0.000150 0.210134 ( 0.210229)
# >> Array#product 0.458375 0.000405 0.458780 ( 0.459150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment