summaryrefslogtreecommitdiff
path: root/lib/vendor/excon/benchmarks/class_vs_lambda.rb
blob: 6f902eeb834f0cf468412696bbb7c1b5f912d8e2 (plain)
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
require 'rubygems'
require 'tach'

class Concatenator
  def initialize(string)
    @string = string
  end

  def call(data)
    @string << data
  end
end

string = "0123456789ABCDEF"

Tach.meter(100_000) do
  tach('class') do
    s = ""
    obj = Concatenator.new(s)
    10.times { obj.call(string) }
  end

  tach('lambda') do
    s = ""
    obj = lambda {|data| s << data }
    10.times { obj.call(string) }
  end
end

# ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
#
#  +--------+----------+
#  | tach   | total    |
#  +--------+----------+
#  | class  | 1.450284 |
#  +--------+----------+
#  | lambda | 2.506496 |
#  +--------+----------+

# ruby 1.8.7 (2010-12-23 patchlevel 330) [x86_64-linux]
#
#  +--------+----------+
#  | tach   | total    |
#  +--------+----------+
#  | class  | 1.373917 |
#  +--------+----------+
#  | lambda | 2.589384 |
#  +--------+----------+