Created
November 3, 2012 19:43
-
-
Save taq/4008453 to your computer and use it in GitHub Desktop.
Ruby 2.0 lazy enumerators small collection performance
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" | |
include Benchmark | |
values = (0..100).to_a | |
bm(10) do |bench| | |
bench.report("regular") do | |
values.map { |x| x * 10 }.select { |x| x > 30 } | |
end | |
bench.report("lazy") do | |
values.lazy.map { |x| x * 10 }.select { |x| x > 30 } | |
end | |
end | |
# user system total real | |
# regular 0.000000 0.000000 0.000000 ( 0.000032) | |
# lazy 0.000000 0.000000 0.000000 ( 0.000018) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgive me for criticizing your gist uninvited, but your benchmark methodology is flawed in the lazy version—since you're never actually forcing evaluation of the enumerator it'll always just return an
Enumerator::Lazy
in constant time, virtually instantly. You need to do something like this to get a more accurate comparison of the two versions: