Skip to content

Instantly share code, notes, and snippets.

@gjcourt
Created August 1, 2019 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjcourt/826c2cd362e36670ab857ac04efbafb1 to your computer and use it in GitHub Desktop.
Save gjcourt/826c2cd362e36670ab857ac04efbafb1 to your computer and use it in GitHub Desktop.
require "benchmark"
$count = 5
$item_count = 100_000
$iterations = 1_000
def setup
(1..$count).map{|x| {:num => x, :data => (1..$item_count).to_a}}
end
def test_one
seq = setup()
Benchmark.measure do
(1..$iterations).each do
res = seq.flat_map{|x| x[:data]}
raise Exception if res.length != $count * $item_count
end
end
end
def test_two
seq = setup()
Benchmark.measure do
(1..$iterations).each do
res = []
seq.each{|x| res += x[:data]}
raise Exception if res.length != $count * $item_count
end
end
end
puts "Ran test one in #{test_one}s"
puts "Ran test two in #{test_two}s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment