Skip to content

Instantly share code, notes, and snippets.

@phildionne
Forked from fbernier/lazy.rb
Last active August 29, 2015 13: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 phildionne/9232896 to your computer and use it in GitHub Desktop.
Save phildionne/9232896 to your computer and use it in GitHub Desktop.
# Enumerator#lazy is very slow and should only be used when iterating over large/infinite collections
# where you know you are going to get your results quite early in the iteration.
require 'benchmark/ips'
Benchmark.ips do |r|
r.report("map") do
(0..50).map{ |i| 'lol' if i.even? }
end
r.report("lazy+map") do
(0..50).lazy.map { |i| 'lol' if i.even? }.to_a
end
end
Calculating -------------------------------------
map 7726 i/100ms
lazy+map 2451 i/100ms
-------------------------------------------------
map 87178.8 (±2.6%) i/s - 440382 in 5.055076s
lazy+map 27059.9 (±2.7%) i/s - 137256 in 5.076049s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment