Skip to content

Commit e5220ef

Browse files
committed
Add 'Begin Until' to the loop battle
1 parent 838a18c commit e5220ef

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

.README.md.swp

64 KB
Binary file not shown.

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,20 @@ Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
174174

175175
```
176176
$ ruby -v code/general/loop-vs-while-true.rb
177-
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
178-
179-
Calculating -------------------------------------
177+
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
178+
Warming up --------------------------------------
179+
Begin Until 1.000 i/100ms
180180
While Loop 1.000 i/100ms
181181
Kernel loop 1.000 i/100ms
182-
-------------------------------------------------
183-
While Loop 0.536 (± 0.0%) i/s - 3.000 in 5.593042s
184-
Kernel loop 0.223 (± 0.0%) i/s - 2.000 in 8.982355s
182+
Calculating -------------------------------------
183+
Begin Until 0.211 (± 0.0%) i/s - 2.000 in 9.469782s
184+
While Loop 0.192 (± 0.0%) i/s - 1.000 in 5.212417s
185+
Kernel loop 0.072 (± 0.0%) i/s - 1.000 in 13.921672s
185186
186187
Comparison:
187-
While Loop: 0.5 i/s
188-
Kernel loop: 0.2 i/s - 2.41x slower
188+
Begin Until: 0.2 i/s
189+
While Loop: 0.2 i/s - 1.10x slower
190+
Kernel loop: 0.1 i/s - 2.94x slower
189191
```
190192

191193
#### Method Invocation

code/general/loop-vs-while-true.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
NUMBER = 100_000_000
44

5+
def fastest
6+
index = 0
7+
begin
8+
index += 1
9+
end until index > NUMBER
10+
end
11+
512
def fast
613
index = 0
714
while true
@@ -19,6 +26,7 @@ def slow
1926
end
2027

2128
Benchmark.ips do |x|
29+
x.report("Begin Until") { fastest }
2230
x.report("While Loop") { fast }
2331
x.report("Kernel loop") { slow }
2432
x.compare!

0 commit comments

Comments
 (0)