Created
February 5, 2009 05:38
-
-
Save Pistos/58562 to your computer and use it in GitHub Desktop.
"while true" is faster than "loop do"
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 'better-benchmark' | |
result = Benchmark.compare_realtime( | |
:iterations => 10, | |
:inner_iterations => 10, | |
:verbose => true | |
) { | |
count = 0 | |
while true | |
count += 1 | |
break if count == 100_000 | |
end | |
}.with { | |
count = 0 | |
loop do | |
count += 1 | |
break if count == 100_000 | |
end | |
} | |
Benchmark.report_on result | |
# % ruby ~/ruby/loop.rb | |
# .......... | |
# Set 1 mean: 0.480 s | |
# Set 1 std dev: 0.002 | |
# Set 2 mean: 0.700 s | |
# Set 2 std dev: 0.013 | |
# p.value: 1.0825088224469e-05 | |
# W: 0.0 | |
# The difference (+45.9%) IS statistically significant. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment