File tree Expand file tree Collapse file tree 3 files changed +19
-9
lines changed Expand file tree Collapse file tree 3 files changed +19
-9
lines changed Original file line number Diff line number Diff line change @@ -170,22 +170,24 @@ Custom exception: Kernel#raise: 589148.7 i/s
170170Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
171171```
172172
173- ##### ` loop ` vs ` while true ` [ code] ( code/general/loop-vs-while-true.rb )
173+ ##### ` loop ` vs ` while true ` vs ` begin..end until ` [ code] ( code/general/loop-vs-while-true.rb )
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
186187Comparison:
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
Original file line number Diff line number Diff line change 22
33NUMBER = 100_000_000
44
5+ def fastest
6+ index = 0
7+ begin
8+ index += 1
9+ end until index > NUMBER
10+ end
11+
512def fast
613 index = 0
714 while true
@@ -19,6 +26,7 @@ def slow
1926end
2027
2128Benchmark . ips do |x |
29+ x . report ( "Begin Until" ) { fastest }
2230 x . report ( "While Loop" ) { fast }
2331 x . report ( "Kernel loop" ) { slow }
2432 x . compare!
You can’t perform that action at this time.
0 commit comments