mirror of
https://github.com/lloeki/ruby-benchmarks.git
synced 2025-12-06 08:54:39 +01:00
Add block vs ensure benchmark
This commit is contained in:
commit
484269dba2
4 changed files with 80 additions and 0 deletions
29
block-ensure/example.rb
Executable file
29
block-ensure/example.rb
Executable file
|
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'benchmark'
|
||||
|
||||
class Example
|
||||
def initialize
|
||||
@mutex = Mutex.new
|
||||
end
|
||||
|
||||
def using_block
|
||||
@mutex.synchronize do
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def using_ensure
|
||||
@mutex.lock
|
||||
ensure
|
||||
@mutex.unlock
|
||||
end
|
||||
end
|
||||
|
||||
Benchmark.bm(12) do |x|
|
||||
ex = Example.new
|
||||
n = 10_000_000
|
||||
|
||||
x.report('block') { n.times { ex.using_block } }
|
||||
x.report('ensure') { n.times { ex.using_ensure } }
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue