-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.rb
120 lines (92 loc) · 1.99 KB
/
benchmark.rb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'benchmark/ips'
require 'ostruct'
require 'ofstruct'
require_relative '../lib/persistent_open_struct'
class RegularClass
attr_accessor :foo
def initialize(args)
@foo = args[:foo]
end
end
puts "Initialization benchmark\n\n"
Benchmark.ips do |x|
input_hash = { foo: :bar }
x.report('OpenStruct') do
OpenStruct.new(input_hash)
end
x.report('PersistentOpenStruct') do
PersistentOpenStruct.new(input_hash)
end
x.report('OpenFastStruct') do
OpenFastStruct.new(input_hash)
end
x.report('RegularClass') do
RegularClass.new(input_hash)
end
x.compare!
end
puts "\n\nAssignment Benchmark\n\n"
Benchmark.ips do |x|
os = OpenStruct.new(foo: :bar)
pos = PersistentOpenStruct.new(foo: :bar)
ofs = OpenFastStruct.new(foo: :bar)
rgc = RegularClass.new(foo: :bar)
x.report('OpenStruct') do
os.foo = :bar
end
x.report('PersistentOpenStruct') do
pos.foo = :bar
end
x.report('OpenFastStruct') do
ofs.foo = :bar
end
x.report('RegularClass') do
rgc.foo = :bar
end
x.compare!
end
puts "\n\nAccess Benchmark\n\n"
Benchmark.ips do |x|
os = OpenStruct.new(foo: :bar)
pos = PersistentOpenStruct.new(foo: :bar)
ofs = OpenFastStruct.new(foo: :bar)
rgc = RegularClass.new(foo: :bar)
x.report('OpenStruct') do
os.foo
end
x.report('PersistentOpenStruct') do
pos.foo
end
x.report('OpenFastStruct') do
ofs.foo
end
x.report('RegularClass') do
pos.foo
end
x.compare!
end
puts "\n\nAll-Together benchmark\n\n"
Benchmark.ips do |x|
input_hash = { foo: :bar }
x.report('OpenStruct') do
os = OpenStruct.new(input_hash)
os.foo = :bar
os.foo
end
x.report('PersistentOpenStruct') do
pos = PersistentOpenStruct.new(input_hash)
pos.foo = :bar
pos.foo
end
x.report('OpenFastStruct') do
ofs = OpenFastStruct.new(input_hash)
ofs.foo = :bar
ofs.foo
end
x.report('RegularClass') do
rgc = RegularClass.new(input_hash)
rgc.foo = :bar
rgc.foo
end
x.compare!
end