Project

General

Profile

Feature #3917 ยป bench.rb

benchmark script - kwatch (makoto kuwata), 10/08/2010 08:45 AM

 
1
##
2
##  usage: ruby -s bench.rb [-N=100000]
3
##
4
##  ex.
5
##     $ ./ruby bench.rb
6
##                                         user     system      total        real
7
##     caller()[0]                     1.890000   0.010000   1.900000 (  1.941812)
8
##     caller()[0] (retrieve)          2.190000   0.010000   2.200000 (  2.225966)
9
##     called_from()                   0.100000   0.000000   0.100000 (  0.102810)
10
##     called_from() (retrieve)        0.100000   0.000000   0.100000 (  0.102133)
11
##
12

    
13
require 'benchmark'
14

    
15
def f1; f2; end
16
def f2; f3; end
17
def f3; f4; end
18
def f4; f5; end
19
def f5; f6; end
20
def f6; f7; end
21
def f7; f8; end
22
def f8; f9; end
23
def f9; f10; end
24

    
25
n = ($N || 100000).to_i
26

    
27
Benchmark.bm(30) do |x|
28

    
29
  ## benchmarks for caller()
30

    
31
  def f10
32
    caller()[0]
33
  end
34

    
35
  x.report("caller()[0]") do
36
    n.times { f1() }
37
  end
38

    
39
  x.report("caller()[0] (retrieve)") do
40
    n.times { f1() =~ /:(\d+)/; fname = $`; lnum = $1.to_i }
41
  end
42

    
43
  ## benchmarks for called_from()
44

    
45
  def f10
46
    called_from()
47
  end
48

    
49
  x.report("called_from()") do
50
    n.times { f1() }
51
  end
52

    
53
  x.report("called_from() (retrieve)") do
54
    n.times { fname, lnum, func_name = f1(); }
55
  end
56

    
57
end