Last active
May 4, 2017 06:55
-
-
Save jorgemanrubia/d1231bb4606fe487d2c1d9b6000bfdaa to your computer and use it in GitHub Desktop.
Ruby enumerators performance
This file contains 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/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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results with Ruby 2.4.0