Skip to content

Instantly share code, notes, and snippets.

@Pistos
Created February 5, 2009 05:38
Show Gist options
  • Save Pistos/58562 to your computer and use it in GitHub Desktop.
Save Pistos/58562 to your computer and use it in GitHub Desktop.
"while true" is faster than "loop do"
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