File: call_v_yield.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (81 lines) | stat: -rw-r--r-- 1,321 bytes parent folder | download | duplicates (6)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'benchmark'

n = 100_000

def call_block(&block)
  block.call
end

def yield_control
  yield
end

Benchmark.benchmark do |bm|
  puts "#{n} times - ruby #{RUBY_VERSION}"

  puts
  puts "eval"

  3.times do
    bm.report do
      n.times do
        eval("2 + 3")
      end
    end
  end

  puts
  puts "call block"

  3.times do
    bm.report do
      n.times do
        call_block { 2 + 3 }
      end
    end
  end

  puts
  puts "yield"

  3.times do
    bm.report do
      n.times do
        yield_control { 2 + 3 }
      end
    end
  end

  puts
  puts "exec"

  3.times do
    bm.report do
      n.times do
        2 + 3
      end
    end
  end
end

# 100000 times - ruby 1.9.3
#
# eval
#    0.870000   0.010000   0.880000 (  0.877762)
#    0.890000   0.000000   0.890000 (  0.891142)
#    0.890000   0.000000   0.890000 (  0.896365)
#
# call block
#    0.120000   0.010000   0.130000 (  0.136322)
#    0.130000   0.010000   0.140000 (  0.138608)
#    0.130000   0.000000   0.130000 (  0.129931)
#
# yield
#    0.020000   0.000000   0.020000 (  0.020412)
#    0.010000   0.000000   0.010000 (  0.017926)
#    0.020000   0.000000   0.020000 (  0.025740)
#
# exec
#    0.010000   0.000000   0.010000 (  0.009935)
#    0.010000   0.000000   0.010000 (  0.011588)
#    0.010000   0.000000   0.010000 (  0.010613)