Skip to content

Commit 39534a4

Browse files
TheSmartnikJuanitoFatas
authored andcommitted
Compare kernel loop and while loop
Closes #79
1 parent 797e2ca commit 39534a4

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ Custom exception: Kernel#raise: 589148.7 i/s
141141
Custom exception: E2MM#Raise: 29004.8 i/s - 20.31x slower
142142
```
143143

144+
##### `loop` vs `while true` [code](code/general/loop-vs-while-true.rb)
145+
146+
```
147+
$ ruby -v code/general/loop-vs-while-true.rb
148+
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
149+
150+
Calculating -------------------------------------
151+
While Loop 1.000 i/100ms
152+
Kernel loop 1.000 i/100ms
153+
-------------------------------------------------
154+
While Loop 0.536 (± 0.0%) i/s - 3.000 in 5.593042s
155+
Kernel loop 0.223 (± 0.0%) i/s - 2.000 in 8.982355s
156+
157+
Comparison:
158+
While Loop: 0.5 i/s
159+
Kernel loop: 0.2 i/s - 2.41x slower
160+
```
161+
144162
#### Method Invocation
145163

146164
##### `call` vs `send` vs `method_missing` [code](code/method/call-vs-send-vs-method_missing.rb)

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "benchmark/ips"
2+
3+
NUMBER = 100_000_000
4+
5+
def fast
6+
index = 0
7+
while true
8+
break if index > NUMBER
9+
index += 1
10+
end
11+
end
12+
13+
def slow
14+
index = 0
15+
loop do
16+
break if index > NUMBER
17+
index += 1
18+
end
19+
end
20+
21+
Benchmark.ips do |x|
22+
x.report("While Loop") { fast }
23+
x.report("Kernel loop") { slow }
24+
x.compare!
25+
end

0 commit comments

Comments
 (0)