Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
Last active May 4, 2017 06:55
Show Gist options
  • Save jorgemanrubia/d1231bb4606fe487d2c1d9b6000bfdaa to your computer and use it in GitHub Desktop.
Save jorgemanrubia/d1231bb4606fe487d2c1d9b6000bfdaa to your computer and use it in GitHub Desktop.
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

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

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