Skip to content

Commit d4be15b

Browse files
committed
Add more examples to infinite loops
1 parent a9e12bc commit d4be15b

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,28 @@ Custom exception: Kernel#raise: 589148.7 i/s
169169
Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
170170
```
171171

172-
##### `loop` vs `while true` [code](code/general/loop-vs-while-true.rb)
172+
##### `loop` vs `while true` vs `until false` vs `infinite range` [code](code/general/loop-vs-while-true-vs-until-false-vs-infinite-range.rb)
173173

174174
```
175-
$ ruby -v code/general/loop-vs-while-true.rb
176-
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
175+
$ ruby -v code/general/loop-vs-while-true-vs-until-false-vs-infinite-range.rb
176+
ruby 3.3.0dev (2023-11-12 master 60e19a0b5f) [x86_64-linux]
177177
178-
Calculating -------------------------------------
179-
While Loop 1.000 i/100ms
178+
Warming up --------------------------------------
179+
While loop 1.000 i/100ms
180+
Until loop 1.000 i/100ms
180181
Kernel loop 1.000 i/100ms
181-
-------------------------------------------------
182-
While Loop 0.536 (± 0.0%) i/s - 3.000 in 5.593042s
183-
Kernel loop 0.223 (± 0.0%) i/s - 2.000 in 8.982355s
182+
Infinite range 1.000 i/100ms
183+
Calculating -------------------------------------
184+
While loop 0.600 (± 0.0%) i/s - 3.000 in 5.002930s
185+
Until loop 0.594 (± 0.0%) i/s - 3.000 in 5.054004s
186+
Kernel loop 0.258 (± 0.0%) i/s - 2.000 in 7.754873s
187+
Infinite range 0.207 (± 0.0%) i/s - 2.000 in 9.664227s
184188
185189
Comparison:
186-
While Loop: 0.5 i/s
187-
Kernel loop: 0.2 i/s - 2.41x slower
190+
While loop: 0.6 i/s
191+
Until loop: 0.6 i/s - 1.01x slower
192+
Kernel loop: 0.3 i/s - 2.33x slower
193+
Infinite range: 0.2 i/s - 2.90x slower
188194
```
189195

190196
##### `ancestors.include?` vs `<=` [code](code/general/inheritance-check.rb)

code/general/loop-vs-while-true.rb renamed to code/general/loop-vs-while-true-vs-until-false-vs-infinite-range.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
NUMBER = 100_000_000
44

5-
def fast
5+
def fastest
66
index = 0
77
while true
88
break if index > NUMBER
99
index += 1
1010
end
1111
end
1212

13+
def fast
14+
index = 0
15+
until false
16+
break if index > NUMBER
17+
index += 1
18+
end
19+
end
20+
1321
def slow
1422
index = 0
1523
loop do
@@ -18,8 +26,16 @@ def slow
1826
end
1927
end
2028

29+
def slowest
30+
(0..).each do |index|
31+
break if index > NUMBER
32+
end
33+
end
34+
2135
Benchmark.ips do |x|
22-
x.report("While Loop") { fast }
36+
x.report("While loop") { fastest }
37+
x.report("Until loop") { fast }
2338
x.report("Kernel loop") { slow }
39+
x.report("Infinite range") { slowest }
2440
x.compare!
2541
end

0 commit comments

Comments
 (0)