Created
July 21, 2021 14:32
-
-
Save francois/66721cccd96ce9d2302e62661b6c48ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "benchmark" | |
srand(42) | |
ARRAY = 1_000_000.times.map { rand }.freeze | |
Benchmark.bmbm do |b| | |
b.report("combined") { ARRAY.select{|val| val < 500 && val > -500 && !val.zero?}.map{|val| -2 * val}.select{|val| val / 2 < 1}.to_a } | |
b.report("separate") { ARRAY.select{|val| val < 500}.select{|val| val > -500}.reject(&:zero?).map{|val| -2 * val}.select{|val| val / 2 < 1}.to_a } | |
b.report("lazy-sep") { ARRAY.lazy.select{|val| val < 500}.select{|val| val > -500}.reject(&:zero?).map{|val| -2 * val}.select{|val| val / 2 < 1}.to_a } | |
b.report("lazy-comb") { ARRAY.lazy.select{|val| val < 500 && val > -500 && !val.zero?}.map{|val| -2 * val}.select{|val| val / 2 < 1}.to_a } | |
b.report("in-place") { x = ARRAY.dup ; x.select!{|val| val < 500 && val > -500 && !val.zero?} ; x.map!{|val| -2 * val} ; x.select{|val| val / 2 < 1} ; x.to_a } | |
end |
This file contains hidden or 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
Rehearsal --------------------------------------------- | |
combined 0.279523 0.008966 0.288489 ( 0.290051) | |
separate 0.337086 0.011403 0.348489 ( 0.349970) | |
lazy-sep 0.490321 0.006044 0.496365 ( 0.498582) | |
lazy-comb 0.409920 0.005352 0.415272 ( 0.416394) | |
in-place 0.255416 0.003981 0.259397 ( 0.260249) | |
------------------------------------ total: 1.808012sec | |
user system total real | |
combined 0.255729 0.000469 0.256198 ( 0.256833) | |
separate 0.327413 0.007568 0.334981 ( 0.336166) | |
lazy-sep 0.486621 0.002601 0.489222 ( 0.490811) | |
lazy-comb 0.402537 0.001565 0.404102 ( 0.405677) | |
in-place 0.260623 0.005263 0.265886 ( 0.267473) |
This file contains hidden or 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 2.7.3p183 (2021-04-05 revision 6847ee089d) [x86_64-darwin20] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment