Pistos (owner)

Forks

Revisions

gist: 58562 Download_button fork
public
Description:
"while true" is faster than "loop do"
Public Clone URL: git://gist.github.com/58562.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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.