Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created May 16, 2014 11:03
Show Gist options
  • Save ifyouseewendy/4cdad525a33be8a1ea53 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/4cdad525a33be8a1ea53 to your computer and use it in GitHub Desktop.
Does Enumerator::Lazy make loops running faster? Maybe, It depends.
# ruby 2.1.2
require 'benchmark/ips'
Benchmark.ips do |x|
x.report { (1..100).select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 } }
x.report { (1..100).select { |x| x % 3 == 0 && x % 4 == 0 } }
x.report { (1..100).lazy.select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 }.to_a }
end
# Calculating -------------------------------------
# 6721 i/100ms 70335.8 (±9.5%) i/s - 349492 in 5.021743s
# 7731 i/100ms 78716.6 (±8.3%) i/s - 394281 in 5.047430s
# 2565 i/100ms 26628.3 (±17.8%) i/s - 130815 in 5.075386s
Benchmark.ips do |x|
x.report { (1..100).select { x.to_s.include?('3') }.take(10) }
x.report { (1..100).lazy.select { x.to_s.include?('3') }.take(10).to_a }
end
# Calculating -------------------------------------
# 702 i/100ms 7259.2 (±12.7%) i/s - 35802 in 5.013737s
# 2614 i/100ms 26893.2 (±15.5%) i/s - 133314 in 5.078519s
# NEED TO RUN MORE TESTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment