diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..dd40e06 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'benchmark-ips' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..73d9550 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + benchmark-ips (2.12.0) + +PLATFORMS + arm64-darwin-22 + +DEPENDENCIES + benchmark-ips + +BUNDLED WITH + 2.3.7 diff --git a/block-ensure/README.md b/block-ensure/README.md index 20c2ab0..f033b77 100644 --- a/block-ensure/README.md +++ b/block-ensure/README.md @@ -1,5 +1,12 @@ ``` - user system total real -block 0.961390 0.008933 0.970323 ( 0.978850) -ensure 0.752044 0.006407 0.758451 ( 0.763439) +Warming up -------------------------------------- + block 1.000 i/100ms + ensure 1.000 i/100ms +Calculating ------------------------------------- + block 1.099 (± 0.0%) i/s - 6.000 in 5.460202s + ensure 1.400 (± 0.0%) i/s - 8.000 in 5.713243s + +Comparison: + ensure: 1.4 i/s + block: 1.1 i/s - 1.27x slower ``` diff --git a/block-ensure/example.rb b/block-ensure/example.rb index eb903a2..3864892 100755 --- a/block-ensure/example.rb +++ b/block-ensure/example.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby +# frozen_string_literal: true -require 'benchmark' +require 'benchmark/ips' class Example def initialize @@ -20,10 +21,15 @@ class Example end end -Benchmark.bm(12) do |x| +Benchmark.ips do |x| + x.time = 5 + x.warmup = 0.5 + ex = Example.new n = 10_000_000 x.report('block') { n.times { ex.using_block } } x.report('ensure') { n.times { ex.using_ensure } } + + x.compare! end