rtype-java 0.6.8-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,200 @@
1
+ require 'benchmark/ips'
2
+
3
+ is_mri = RUBY_ENGINE == 'ruby'
4
+
5
+ require "rtype"
6
+ require "rubype" if is_mri
7
+ require "sig"
8
+ require "contracts"
9
+ require "contracts/version"
10
+ require "typecheck"
11
+
12
+ puts "Ruby version: #{RUBY_VERSION}"
13
+ puts "Ruby engine: #{RUBY_ENGINE}"
14
+ puts "Ruby description: #{RUBY_DESCRIPTION}"
15
+
16
+ puts "Rtype version: #{Rtype::VERSION}"
17
+ puts "Rubype version: #{Rubype::VERSION}" if is_mri
18
+ puts "Sig version: #{Sig::VERSION}"
19
+ puts "Contracts version: #{Contracts::VERSION}"
20
+ puts "Typecheck version: #{Typecheck::VERSION}"
21
+
22
+ if !Rtype::NATIVE_EXT_VERSION.nil?
23
+ puts "Rtype with native extension"
24
+ elsif !Rtype::JAVA_EXT_VERSION.nil?
25
+ puts "Rtype with java extension"
26
+ else
27
+ puts "Rtype without native extension"
28
+ end
29
+
30
+ class PureTest
31
+ def sum(x, y)
32
+ x + y
33
+ end
34
+
35
+ def mul(x, y)
36
+ x * y
37
+ end
38
+
39
+ def args(a, b, c, d)
40
+ end
41
+ end
42
+ pure_obj = PureTest.new
43
+
44
+ class RtypeTest
45
+ rtype [Numeric, Numeric] => Numeric
46
+ def sum(x, y)
47
+ x + y
48
+ end
49
+
50
+ rtype [:to_i, :to_i] => Numeric
51
+ def mul(x, y)
52
+ x * y
53
+ end
54
+
55
+ rtype [Integer, Numeric, String, :to_i] => Any
56
+ def args(a, b, c, d)
57
+ end
58
+ end
59
+ rtype_obj = RtypeTest.new
60
+
61
+ if is_mri
62
+ class RubypeTest
63
+ def sum(x, y)
64
+ x + y
65
+ end
66
+ typesig :sum, [Numeric, Numeric] => Numeric
67
+
68
+ def mul(x, y)
69
+ x * y
70
+ end
71
+ typesig :mul, [:to_i, :to_i] => Numeric
72
+
73
+ def args(a, b, c, d)
74
+ end
75
+ typesig :args, [Integer, Numeric, String, :to_i] => Any
76
+ end
77
+ rubype_obj = RubypeTest.new
78
+ end
79
+
80
+ class SigTest
81
+ sig [Numeric, Numeric], Numeric,
82
+ def sum(x, y)
83
+ x + y
84
+ end
85
+
86
+ sig [:to_i, :to_i], Numeric,
87
+ def mul(x, y)
88
+ x * y
89
+ end
90
+
91
+ # nil means wildcard
92
+ sig [Integer, Numeric, String, :to_i], nil,
93
+ def args(a, b, c, d)
94
+ end
95
+ end
96
+ sig_obj = SigTest.new
97
+
98
+ class ContractsTest
99
+ include Contracts
100
+
101
+ Contract Num, Num => Num
102
+ def sum(x, y)
103
+ x + y
104
+ end
105
+
106
+ Contract RespondTo[:to_i], RespondTo[:to_i] => Num
107
+ def mul(x, y)
108
+ x * y
109
+ end
110
+
111
+ Contract Int, Num, String, RespondTo[:to_i] => Any
112
+ def args(a, b, c, d)
113
+ end
114
+ end
115
+ contracts_obj = ContractsTest.new
116
+
117
+ class TypecheckTest
118
+ extend Typecheck
119
+
120
+ typecheck 'Numeric, Numeric -> Numeric',
121
+ def sum(x, y)
122
+ x + y
123
+ end
124
+
125
+ typecheck '#to_i, #to_i -> Numeric',
126
+ def mul(x, y)
127
+ x * y
128
+ end
129
+
130
+ typecheck 'Integer, Numeric, String, #to_i -> BasicObject',
131
+ def args(a, b, c, d)
132
+ end
133
+ end
134
+ typecheck_obj = TypecheckTest.new
135
+
136
+ Benchmark.ips do |x|
137
+ x.report("pure") do |times|
138
+ i = 0
139
+ while i < times
140
+ pure_obj.sum(1, 2)
141
+ pure_obj.mul(1, 2)
142
+ pure_obj.args(1, 2, "c", 4)
143
+ i += 1
144
+ end
145
+ end
146
+
147
+ x.report("rtype") do |times|
148
+ i = 0
149
+ while i < times
150
+ rtype_obj.sum(1, 2)
151
+ rtype_obj.mul(1, 2)
152
+ rtype_obj.args(1, 2, "c", 4)
153
+ i += 1
154
+ end
155
+ end
156
+
157
+ if is_mri
158
+ x.report("rubype") do |times|
159
+ i = 0
160
+ while i < times
161
+ rubype_obj.sum(1, 2)
162
+ rubype_obj.mul(1, 2)
163
+ rubype_obj.args(1, 2, "c", 4)
164
+ i += 1
165
+ end
166
+ end
167
+ end
168
+
169
+ x.report("sig") do |times|
170
+ i = 0
171
+ while i < times
172
+ sig_obj.sum(1, 2)
173
+ sig_obj.mul(1, 2)
174
+ sig_obj.args(1, 2, "c", 4)
175
+ i += 1
176
+ end
177
+ end
178
+
179
+ x.report("contracts") do |times|
180
+ i = 0
181
+ while i < times
182
+ contracts_obj.sum(1, 2)
183
+ contracts_obj.mul(1, 2)
184
+ contracts_obj.args(1, 2, "c", 4)
185
+ i += 1
186
+ end
187
+ end
188
+
189
+ x.report("typecheck") do |times|
190
+ i = 0
191
+ while i < times
192
+ typecheck_obj.sum(1, 2)
193
+ typecheck_obj.mul(1, 2)
194
+ typecheck_obj.args(1, 2, "c", 4)
195
+ i += 1
196
+ end
197
+ end
198
+
199
+ x.compare!
200
+ end
Binary file