Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Begin Until' to the loop battle #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .README.md.swp
Binary file not shown.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,24 @@ Custom exception: Kernel#raise: 589148.7 i/s
Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
```

##### `loop` vs `while true` [code](code/general/loop-vs-while-true.rb)
##### `loop` vs `while true` vs `begin..end until` [code](code/general/loop-vs-while-true.rb)

```
$ ruby -v code/general/loop-vs-while-true.rb
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

Calculating -------------------------------------
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
Warming up --------------------------------------
Begin Until 1.000 i/100ms
While Loop 1.000 i/100ms
Kernel loop 1.000 i/100ms
-------------------------------------------------
While Loop 0.536 (± 0.0%) i/s - 3.000 in 5.593042s
Kernel loop 0.223 (± 0.0%) i/s - 2.000 in 8.982355s
Calculating -------------------------------------
Begin Until 0.211 (± 0.0%) i/s - 2.000 in 9.469782s
While Loop 0.192 (± 0.0%) i/s - 1.000 in 5.212417s
Kernel loop 0.072 (± 0.0%) i/s - 1.000 in 13.921672s

Comparison:
While Loop: 0.5 i/s
Kernel loop: 0.2 i/s - 2.41x slower
Begin Until: 0.2 i/s
While Loop: 0.2 i/s - 1.10x slower
Kernel loop: 0.1 i/s - 2.94x slower
```

#### Method Invocation
Expand Down
8 changes: 8 additions & 0 deletions code/general/loop-vs-while-true.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

NUMBER = 100_000_000

def fastest
index = 0
begin
index += 1
end until index > NUMBER
end

def fast
index = 0
while true
Expand All @@ -19,6 +26,7 @@ def slow
end

Benchmark.ips do |x|
x.report("Begin Until") { fastest }
x.report("While Loop") { fast }
x.report("Kernel loop") { slow }
x.compare!
Expand Down