Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby enumerators performance
require 'benchmark/ips'
COUNT = 500000
Benchmark.ips do |x|
x.report('Enumerable') do
total = 0
COUNT.times do |i|
total += i
end
end
x.report('Enumerator') do
total = 0
enumerator = COUNT.times
while true
begin
total += enumerator.next
rescue StopIteration
break
end
end
end
end
@jorgemanrubia
Copy link
Author

jorgemanrubia commented May 4, 2017

Results with Ruby 2.4.0

Enumerable     37.073  (± 5.4%) i/s -    186.000  in   5.030412s
Enumerator      1.588  (± 0.0%) i/s -      8.000  in   5.040230s

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment