Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## Result [plain_text]

              user     system      total        real
   yield 18.300000   0.120000  18.420000 ( 19.408501)
   block 41.890000   0.320000  42.210000 ( 44.572319)
Proc.new 38.560000   0.280000  38.840000 ( 41.083233)

## Benchmarking code [ruby]

require 'benchmark'
TIMES = 10_000_000

def thrice_yield
  3.times { yield }
end

def thrice_block &block
  3.times &block
end

def thrice_proc_dot_new
  3.times &Proc.new # Yes, you can do that.
end

print "        " # Prettiness :3

Benchmark.bm do |bm|
  bm.report "   yield" do
    TIMES.times do
      thrice_yield { a = 1 }
    end
  end
  
  bm.report "   block" do
    TIMES.times do
      thrice_block { a = 1 }
    end
  end
  
  bm.report "Proc.new" do
    TIMES.times do
      thrice_proc_dot_new { a = 1 }
    end
  end
end