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 more examples to infinite loops #228

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
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,28 @@ 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 `until false` vs `infinite range` [code](code/general/loop-vs-while-true-vs-until-false-vs-infinite-range.rb)

```
$ ruby -v code/general/loop-vs-while-true.rb
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
$ ruby -v code/general/loop-vs-while-true-vs-until-false-vs-infinite-range.rb
ruby 3.3.0dev (2023-11-12 master 60e19a0b5f) [x86_64-linux]

Calculating -------------------------------------
While Loop 1.000 i/100ms
Warming up --------------------------------------
While loop 1.000 i/100ms
Until 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
Infinite range 1.000 i/100ms
Calculating -------------------------------------
While loop 0.600 (± 0.0%) i/s - 3.000 in 5.002930s
Until loop 0.594 (± 0.0%) i/s - 3.000 in 5.054004s
Kernel loop 0.258 (± 0.0%) i/s - 2.000 in 7.754873s
Infinite range 0.207 (± 0.0%) i/s - 2.000 in 9.664227s

Comparison:
While Loop: 0.5 i/s
Kernel loop: 0.2 i/s - 2.41x slower
While loop: 0.6 i/s
Until loop: 0.6 i/s - 1.01x slower
Kernel loop: 0.3 i/s - 2.33x slower
Infinite range: 0.2 i/s - 2.90x slower
```

##### `ancestors.include?` vs `<=` [code](code/general/inheritance-check.rb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

NUMBER = 100_000_000

def fast
def fastest
index = 0
while true
break if index > NUMBER
index += 1
end
end

def fast
index = 0
until false
break if index > NUMBER
index += 1
end
end

def slow
index = 0
loop do
Expand All @@ -18,8 +26,16 @@ def slow
end
end

def slowest
(0..).each do |index|
break if index > NUMBER
end
end

Benchmark.ips do |x|
x.report("While Loop") { fast }
x.report("While loop") { fastest }
x.report("Until loop") { fast }
x.report("Kernel loop") { slow }
x.report("Infinite range") { slowest }
x.compare!
end