File: ivar_access_bench.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (79 lines) | stat: -rw-r--r-- 2,127 bytes parent folder | download | duplicates (5)
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
require 'benchmark'


SIMPLE_TIMES = 10_000_000
GROWTH_TIMES =      8_000

# prepare dummy objects
default_object = Class.new.new
default_object.instance_variable_set(:@foo,2)

disposable_objects = (0..10).map{Class.new.new}

simple_reader = proc do |o|
  result = nil;
  for i in (0..SIMPLE_TIMES)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
    result = o.instance_variable_get(:@foo)
  end
  result
end

simple_writer = proc do |o|
  for i in (0..SIMPLE_TIMES)
    o.instance_variable_set(:@foo,0)
    o.instance_variable_set(:@foo,1)
    o.instance_variable_set(:@foo,2)
    o.instance_variable_set(:@foo,3)
    o.instance_variable_set(:@foo,4)
    o.instance_variable_set(:@foo,5)
    o.instance_variable_set(:@foo,6)
    o.instance_variable_set(:@foo,7)
    o.instance_variable_set(:@foo,8)
    o.instance_variable_set(:@foo,9)
  end
end

Benchmark.bmbm do |b|
  b.report "baseline x#{SIMPLE_TIMES}" do
    for i in (0..SIMPLE_TIMES)
      1
    end
  end

  b.report "single threaded reads x#{SIMPLE_TIMES}" do
    simple_reader.call(default_object)
  end
  
  b.report "single threaded writes x#{SIMPLE_TIMES}" do
    simple_writer.call(default_object)
  end
  
  b.report "two reader threads x#{SIMPLE_TIMES}" do
    t1 = Thread.new{simple_reader.call(default_object)}
    t2 = Thread.new{simple_reader.call(default_object)}
    t1.join;t2.join
  end
  
  b.report "one reader, one writer x#{SIMPLE_TIMES}" do
    t1 = Thread.new{simple_reader.call(default_object)}
    t2 = Thread.new{simple_writer.call(default_object)}
    t1.join;t2.join
  end

  b.report "single threaded growth x#{GROWTH_TIMES}" do
    o = disposable_objects.pop
    (0..GROWTH_TIMES).each{|i| o.instance_variable_set(:"@bar_#{i}",1)}
  end
  

end