3 # Microbenchmak generation for multiple language
4 # Just a quick an dirty Nit script file :)
6 # This benchmark focuses on effects of the hierarchy dept
7 # on the typetest performances
10 # class Klass[E] super Root
11 # class C1...CX super Root
12 # Klass[CX] isa Klass[C1]
16 var supers
= new Array[Klass]
17 var all_supers
= new HashSet[Klass]
26 var classes
= new Array[Klass]
29 var loops
writable = 50000
30 var middle
writable = 0
31 var dry
writable = false
32 var check
writable = false
36 var s
: nullable Klass = null
44 c
.all_supers
.add_all
(s
.all_supers
)
48 middle
= (dept
+ 1) / 2
51 var file
: nullable OFStream = null
52 fun write
(str
: String)
58 fun initnit
(res
: Array[String]) is abstract
59 fun testnit
: String do return "true"
61 fun writenit
(dir
: String, name
: String)
65 file
= new OFStream.open
("{dir}/{name}.nit")
67 write
"class Root\n\tfun id: Int do return 0\nend"
70 if c
.supers
.is_empty
then
72 else for s
in c
.supers
do
73 write
"\tsuper {s}[E]"
75 write
"\tredef fun id do return {c.id}"
79 write
"fun test(a,b: Root, loops, start: Int)"
83 write
"while i < loops do"
85 write
"\twhile j < loops do"
87 if not dry
then test
= testnit
88 write
"\t\tif {test} and x >= 0 then"
89 if check
then write
"\t\tx += 1"
91 write
"\t\t\tx = x - 1 + i - j"
101 var ia
= new Array[String]
103 write
"var a: Root = {ia.first}"
104 write
"var b: Root = a"
106 write
"\t\t\tif a.id > 0 then a = {i}"
108 write
"test(b, b, 10, -100)"
109 write
"test(a, a, {loops}, 0)"
114 fun initjava
(res
: Array[String], interfaces
: Bool) is abstract
115 fun testjava
(interfaces
: Bool): String do return "true"
116 fun writejava
(dir
: String, name
: String, interfaces
: Bool)
120 file
= new OFStream.open
("{dir}/{name}.java")
123 if interfaces
then cl
= "X"
124 write
"class {name} \{"
126 write
"static interface Root\n\t\{ int id(); \}"
128 write
"static class Root\n\t\{ int id() \{ return 0;\} \}"
132 write
"static interface {c}<E> "
134 write
"static class {c}<E> "
136 if c
.supers
.is_empty
then
137 write
"\textends Root"
138 else for s
in [c
.supers
.first
] do
139 write
"\textends {s}<E>"
143 write
"static class X{c}<E> implements {c}<E>"
146 write
"\tpublic int id() \{ return {c.id}; \}"
150 write
"static public void main(String args[]) \{"
151 var ia
= new Array[String]
152 initjava
(ia
, interfaces
)
153 write
"Root a = {ia.first};"
156 write
"\t\t\tif (a.id() > 0) a = {i};"
158 write
"\ttest(b, b, 10, -100);"
159 write
"\ttest(a, a, {loops}, 0);"
162 write
"static public void test(Root a, Root b, int loops, int start) \{"
163 write
"\tint x = start;"
164 write
"\tfor(int i = 0; i < loops; i++) \{"
165 write
"\t\tfor(int j = 0; j < loops; j++) \{"
167 if not dry
then test
= testjava
(interfaces
)
168 write
"\t\t\tif({test} && x>=0) \{"
169 if check
then write
"\t\t\t\tx = x + 1;"
170 write
"\t\t\t\} else \{ x = x - 1 + i - j; a = b;\}"
171 #write "\t\t\t\} else \{ x = x - 1; a = b;\}"
174 write
"\tSystem.out.println(x);"
180 fun initcsharp
(res
: Array[String], interfaces
: Bool) is abstract
181 fun testcsharp
(interfaces
: Bool): String do return "true"
182 fun writecsharp
(dir
: String, name
: String, interfaces
: Bool)
186 file
= new OFStream.open
("{dir}/{name}.cs")
189 if interfaces
then cl
= "X"
190 write
"class {name} \{"
192 write
"interface Root\n\t\{ int Id(); \}"
194 write
"class Root\n\t\{ public int Id() \{ return 0;\} \}"
198 write
"interface {c}<out E> "
200 write
"class {c}<E> "
202 if c
.supers
.is_empty
then
204 else for s
in [c
.supers
.first
] do
209 write
"class X{c}<E> : {c}<E>"
212 write
"\tpublic int Id() \{ return {c.id}; \}"
216 write
"static void Main(string[] args) \{"
217 var ia
= new Array[String]
218 initcsharp
(ia
, interfaces
)
219 write
"Root a = {ia.first};"
222 write
"\t\t\tif (a.Id() > 0) a = {i};"
224 write
"\tTest(b, b, 10, -100);"
225 write
"\tTest(a, a, {loops}, 0);"
228 write
"static void Test(Root a, Root b, int loops, int start) \{"
229 write
"\tint x = start;"
230 write
"\tfor(int i = 0; i < loops; i++) \{"
231 write
"\t\tfor(int j = 0; j < loops; j++) \{"
233 if not dry
then test
= testcsharp
(interfaces
)
234 write
"\t\t\tif({test} && x>=0) \{"
235 if check
then write
"\t\t\t\tx++;"
236 write
"\} else \{ x = x - 1 + i - j; a = b; \};"
239 write
"\tSystem.Console.WriteLine(x);"
245 fun initscala
(res
: Array[String], interfaces
: Bool) is abstract
246 fun testscala
(interfaces
: Bool): String do return "true"
247 fun writescala
(dir
: String, name
: String, interfaces
: Bool)
251 file
= new OFStream.open
("{dir}/{name}.scala")
254 write
"object {name} \{"
255 write
"class Root\n\t\{ def id: Int = 0 \}"
258 write
"trait {c}[+E] "
260 write
"class {c}[+E] "
262 if c
.supers
.is_empty
then
263 write
"\textends Root"
264 else for s
in [c
.supers
.first
] do
265 write
"\textends {s}[E]"
269 write
"class X{c}[E] extends {c}[E]"
272 write
"\toverride def id: Int = {c.id}"
276 write
"def main(args: Array[String]) = \{"
277 var ia
= new Array[String]
278 initscala
(ia
, interfaces
)
279 write
"var a: Root = {ia.first};"
280 write
"var b: Root = a;"
282 write
"\t\t\tif (a.id > 0) a = {i};"
284 write
"\ttest(b, b, 10, -100)"
285 write
"\ttest(a, a, {loops}, 0)"
288 write
"def test(aa:Root, b:Root, l: Int, start: Int) = \{"
290 write
"\tvar x = start"
291 write
"\tvar loops = l"
293 write
"\twhile (i < loops) \{"
294 write
"\t\tvar j = 0"
295 write
"\t\twhile (j < loops) \{"
297 if not dry
then test
= testscala
(interfaces
)
298 write
"\t\tif ({test} && x>=0) \{"
299 if check
then write
"\t\t\tx += 1;"
300 #write "\} else \{ x = x - 1 + i - j; a = b; \}"
301 write
"\} else \{ x = x - 1; a = b; \}"
306 write
"\t\t\tprintln(x)"
313 fun initcpp
(res
: Array[String]) is abstract
314 fun testcpp
: String do return "true"
315 fun writecpp
(dir
: String, name
: String)
319 file
= new OFStream.open
("{dir}/{name}.cpp")
321 write
"#include <iostream>"
322 write
"#include <stdlib.h>"
323 write
"class Root\n\t\{ public: virtual int id() \{ return 0;\} \};"
325 write
"template<class E>"
327 if c
.supers
.is_empty
then
328 write
"\t: public virtual Root"
329 else for s
in [c
.supers
.first
] do
330 write
"\t: public virtual {s}<E>"
333 write
"\tpublic: virtual int id() \{ return {c.id}; \}"
337 write
"void test(Root *a, Root *b, int loops, int start) \{"
338 write
"\tint x = start;"
339 write
"\tfor(int i = 0; i < loops; i++) \{"
340 write
"\t\tfor(int j = 0; j < loops; j++) \{"
342 if not dry
then test
= testcpp
343 write
"\t\tif({test} && x>=0) \{"
344 if check
then write
"\t\t\tx += 1;"
345 write
"\} else \{ x = x - 1 + i - j; a = b;\}"
348 write
"\tstd::cout << x << std::endl;"
351 write
"int main(int argc, char **argv) \{"
352 var ia
= new Array[String]
354 write
"Root *a = {ia.first};"
357 write
"\t\t\tif (a->id() > 0) a = {i};"
359 write
"\ttest(b, b, 10, -100);"
360 write
"\ttest(a, a, {loops}, 0);"
366 fun inite
(res
: Array[String], se
: Bool) is abstract
367 fun teste
(se
: Bool): String do return "true"
368 fun locale
(se
: Bool) do end
369 fun writee
(dir
: String, name
: String, se
: Bool)
372 dir
= "{dir}/se/{name}"
374 dir
= "{dir}/es/{name}"
377 file
= new OFStream.open
("{dir}/root.e")
380 if se
then istk
= " is"
382 write
"feature id: INTEGER {istk} do Result := 0 end"
387 file
= new OFStream.open
("{dir}/{c}.e")
388 write
"class {c}[E] "
389 if c
.supers
.is_empty
then
390 write
"\tinherit ROOT"
391 else for s
in [c
.supers
.first
] do
392 write
"\tinherit {s}[E]"
394 write
"\t\tredefine id end"
396 write
"\tid: INTEGER {istk} do Result := {c.id} end"
401 file
= new OFStream.open
("{dir}/app{name}.e")
402 write
"class APP{name.to_upper}"
404 write
"insert ARGUMENTS"
412 write
"\tmake(args: ARRAY[STRING]){istk}"
415 write
"\t\t\ta: ROOT"
416 write
"\t\t\tb: ROOT"
418 var ia
= new Array[String]
423 write
"\t\t\tif a.id > 0 then {i} end"
425 write
"\t\t\ttest(b, b, 10, -100)"
426 write
"\t\t\ttest(a, a, {loops}, 0)"
429 write
"\ttest(aa: ROOT; b: ROOT; l: INTEGER; start: INTEGER){istk}"
431 write
"\t\t\ta: ROOT"
432 write
"\t\t\ti: INTEGER"
433 write
"\t\t\tj: INTEGER"
434 write
"\t\t\tx: INTEGER"
436 write
"\t\t\tloops: INTEGER"
438 write
"\t\t\ta := aa"
439 write
"\t\t\tx := start"
440 write
"\t\t\tloops := l"
441 write
"\t\t\tfrom i := 0 until i>=loops loop"
442 write
"\t\t\t\tfrom j := 0 until j>=loops loop"
444 if not dry
then test
= teste
(se
)
445 write
"\t\t\t\t\tif {test} and then x >= 0 then"
446 if check
then write
"\t\t\t\t\tx := x + 1"
447 write
"\t\t\t\t\telse x := x - 1 + i - j; a := b end"
448 write
"\t\t\t\t\tj := j + 1"
450 write
"\t\t\t\ti := i + 1"
452 write
"\t\t\tprint(x.out)"
453 write
"\t\t\tprint(\"%N\
")"
464 var outdir
= args
.first
466 var use_interfaces
= true
467 if args
.length
> 2 then g
.dept
= args
[2].to_i
468 if args
.length
> 3 then use_interfaces
= false
472 g
.writenit
(outdir
, name
)
473 g
.writejava
(outdir
, name
, use_interfaces
)
474 g
.writecsharp
(outdir
, name
, use_interfaces
)
475 g
.writescala
(outdir
, name
, use_interfaces
)
476 g
.writecpp
(outdir
, name
)
477 g
.writee
(outdir
, "{name}_se", true)
478 g
.writee
(outdir
, name
, false)
482 var g
= new Generator