Created
April 11, 2019 21:10
-
-
Save carpodaster/585ff367c69f799a2b0b271d2c847651 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ruby version: 2.6.1 | |
Run with: `ruby pirate_combo.rb` | |
user system total real | |
nested map, small array 0.009232 0.000084 0.009316 ( 0.009315) | |
nested map, huge array 27.279118 0.510522 27.789640 ( 27.790889) | |
flat map, small array 0.002179 0.000043 0.002222 ( 0.002219) | |
flat map, huge array 12.483256 0.087956 12.571212 ( 12.571798) | |
Array#product, small array 0.001514 0.000000 0.001514 ( 0.001513) | |
Array#product, huge array 5.079583 0.000002 5.079585 ( 5.080144) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "minitest/autorun" | |
require "benchmark" | |
# re: https://twitter.com/funktoriell/status/1111360309598531589 | |
describe "the thing" do | |
let(:people) { %w(Guybrush Elaine LeChuck) } | |
let(:drinks) { %w(Coffee Water Rum) } | |
let(:comboƛ) { lambda { |who, what| { who: who, what: what } } } | |
let(:expectation) do | |
[ | |
{ who: "Guybrush", what: "Coffee" }, | |
{ who: "Guybrush", what: "Water" }, | |
{ who: "Guybrush", what: "Rum" }, | |
{ who: "Elaine", what: "Coffee" }, | |
{ who: "Elaine", what: "Water" }, | |
{ who: "Elaine", what: "Rum" }, | |
{ who: "LeChuck", what: "Coffee" }, | |
{ who: "LeChuck", what: "Water" }, | |
{ who: "LeChuck", what: "Rum" }, | |
] | |
end | |
describe "with deep nesting" do | |
subject do | |
people.map { |pirate| drinks.map { |beverage| comboƛ.(pirate, beverage) } }.flatten | |
end | |
it { subject.must_equal expectation } | |
end | |
describe "with #flat_map" do | |
subject do | |
people.flat_map { |pirate| drinks.map { |beverage| comboƛ.(pirate, beverage) } } | |
end | |
it { subject.must_equal expectation } | |
end | |
describe "using Array#product" do | |
subject do | |
people.product(drinks).map { |a, b| comboƛ.(a, b) } | |
end | |
it { subject.must_equal expectation } | |
end | |
end | |
iterations = 50 | |
outer_small = ("A".."Z").to_a | |
inner_small = outer_small.dup | |
outer_medium = ("A".."AZZ").to_a | |
inner_medium = outer_medium.dup | |
Benchmark.bm do |bm| | |
bm.report("nested map, small array") do | |
iterations.times do | |
outer_small.map { |a| inner_small.map { |b| [a, b] } }.flatten | |
end | |
end | |
bm.report("nested map, huge array") do | |
iterations.times do | |
outer_medium.map { |a| inner_medium.map { |b| [a, b] } }.flatten | |
end | |
end | |
bm.report("flat map, small array") do | |
iterations.times do | |
outer_small.flat_map { |a| inner_small.map { |b| [a, b] } } | |
end | |
end | |
bm.report("flat map, huge array") do | |
iterations.times do | |
outer_medium.flat_map { |a| inner_medium.map { |b| [a, b] } } | |
end | |
end | |
bm.report("Array#product, small array") do | |
iterations.times do | |
outer_small.product(inner_small) | |
end | |
end | |
bm.report("Array#product, huge array") do | |
iterations.times do | |
outer_medium.product(inner_medium) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment