Sha256: bb18a4efecabd95ce618cdffba19fc076f654a5d69914f345fd1d9b543c27b1d

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

=begin

Some speed comparisions between yield vs. block.

=end

require 'benchmark'

$n = 10000
$b = proc { 1 }

# standard array

def with_yield
  yield
end

def with_block( &yld )
  yld.call
end

def with_bracket( &yld )
  yld[]
end

# bench calls

def call_with_yield
  $n.times do
    with_yield { 1 } 
  end
end

def call_with_yield_on_proc
  $n.times do
    with_yield &$b
  end
end

def call_with_block
  $n.times do
    with_block { 1 }
  end
end

def call_with_block_on_proc
  $n.times do
    with_block &$b
  end
end

def call_with_bracket
  $n.times do
    with_bracket { 1 }
  end
end

def call_with_bracket_on_proc
  $n.times do
    with_bracket &$b
  end
end

### --- bench ---

puts "\nYield vs. Block"
Benchmark.bm(15) do |b|
  b.report("block           :") { call_with_block }
  b.report("block a proc    :") { call_with_block_on_proc }
  b.report("bracket         :") { call_with_bracket }
  b.report("bracket a proc  :") { call_with_bracket_on_proc }
  b.report("yield           :") { call_with_yield }
  b.report("yield a proc    :") { call_with_yield_on_proc }
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
facets-2.9.2 work/bench/general/bench_yeild.rb
facets-2.3.0 demo/bench/general/bench_yeild.rb
facets-2.4.0 demo/bench/general/bench_yeild.rb
facets-2.4.1 demo/bench/general/bench_yeild.rb
facets-2.4.2 demo/bench/general/bench_yeild.rb
facets-2.4.3 demo/bench/general/bench_yeild.rb
facets-2.4.4 demo/bench/general/bench_yeild.rb
facets-2.4.5 demo/bench/general/bench_yeild.rb