diff --git a/.README.md.swp b/.README.md.swp new file mode 100644 index 0000000..e8de7d1 Binary files /dev/null and b/.README.md.swp differ diff --git a/README.md b/README.md index cfc91b9..88945fa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/code/general/loop-vs-while-true.rb b/code/general/loop-vs-while-true.rb index 33ae139..b5793a3 100644 --- a/code/general/loop-vs-while-true.rb +++ b/code/general/loop-vs-while-true.rb @@ -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 @@ -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!