File tree Expand file tree Collapse file tree 3 files changed +18
-8
lines changed Expand file tree Collapse file tree 3 files changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -174,18 +174,20 @@ Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
174
174
175
175
```
176
176
$ 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
180
180
While Loop 1.000 i/100ms
181
181
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
185
186
186
187
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
189
191
```
190
192
191
193
#### Method Invocation
Original file line number Diff line number Diff line change 2
2
3
3
NUMBER = 100_000_000
4
4
5
+ def fastest
6
+ index = 0
7
+ begin
8
+ index += 1
9
+ end until index > NUMBER
10
+ end
11
+
5
12
def fast
6
13
index = 0
7
14
while true
@@ -19,6 +26,7 @@ def slow
19
26
end
20
27
21
28
Benchmark . ips do |x |
29
+ x . report ( "Begin Until" ) { fastest }
22
30
x . report ( "While Loop" ) { fast }
23
31
x . report ( "Kernel loop" ) { slow }
24
32
x . compare!
You can’t perform that action at this time.
0 commit comments