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]
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 writenit
(dir
: String, name
: String)
62 file
= new OFStream.open
("{dir}/{name}.nit")
64 write
"class Root\n\tfun id: Int do return 0\nend"
67 if c
.supers
.is_empty
then
69 else for s
in c
.supers
do
70 write
"\tsuper {s}[E]"
72 write
"\tredef fun id do return {c.id}"
76 write
"fun test(a,b: Root, loops, start: Int)"
80 write
"while i < loops do"
82 write
"\twhile j < loops do"
84 if dry
then test
= "" else test
= "a isa {classes[middle]}[Root] and "
85 write
"\t\tif {test}x >= 0 then"
86 if check
then write
"\t\tx += 1"
98 write
"var a: Root = new {classes.first}[Root]"
99 write
"var b: Root = a"
101 write
"\t\t\tif a.id > 0 then a = new {c}[Root]"
103 write
"test(b, b, 10, -100)"
104 write
"test(a, a, {loops}, 0)"
109 fun writejava
(dir
: String, name
: String, interfaces
: Bool)
113 file
= new OFStream.open
("{dir}/{name}.java")
116 if interfaces
then cl
= "X"
117 write
"class {name} \{"
119 write
"static interface Root\n\t\{ int id(); \}"
121 write
"static class Root\n\t\{ int id() \{ return 0;\} \}"
125 write
"static interface {c}<E> "
127 write
"static class {c}<E> "
129 if c
.supers
.is_empty
then
130 write
"\textends Root"
131 else for s
in [c
.supers
.first
] do
132 write
"\textends {s}<E>"
136 write
"static class X{c}<E> implements {c}<E>"
139 write
"\tpublic int id() \{ return {c.id}; \}"
143 write
"static public void main(String args[]) \{"
145 if interfaces
then tagc
= "X"
146 write
"\tRoot a = new {tagc}{classes.first}<Root>();"
147 write
"\tRoot b = a;"
149 write
"\t\t\tif (a.id() > 0) a = new {tagc}{c}<Root>();"
151 write
"\ttest(b, b, 10, -100);"
152 write
"\ttest(a, a, {loops}, 0);"
155 write
"static public void test(Root a, Root b, int loops, int start) \{"
156 write
"\tint x = start;"
157 write
"\tfor(int i = 0; i < loops; i++) \{"
158 write
"\t\tfor(int j = 0; j < loops; j++) \{"
160 if dry
then test
= "" else test
= "a instanceof {classes[middle]} && "
161 write
"\t\t\tif({test}x>=0) \{"
162 if check
then write
"\t\t\t\tx -= 2"
163 write
"\t\t\t\} else \{ x--; a = b;\}"
166 write
"\tSystem.out.println(x);"
172 fun writecsharp
(dir
: String, name
: String, interfaces
: Bool)
176 file
= new OFStream.open
("{dir}/{name}.cs")
179 if interfaces
then cl
= "X"
180 write
"class {name} \{"
182 write
"interface Root\n\t\{ int Id(); \}"
184 write
"class Root\n\t\{ public int Id() \{ return 0;\} \}"
188 write
"interface {c}<E> "
190 write
"class {c}<E> "
192 if c
.supers
.is_empty
then
194 else for s
in [c
.supers
.first
] do
199 write
"class X{c}<E> : {c}<E>"
202 write
"\tpublic int Id() \{ return {c.id}; \}"
206 write
"static void Main(string[] args) \{"
208 if interfaces
then tagc
= "X"
209 write
"\tRoot a = new {tagc}{classes.first}<Root>();"
210 write
"\tRoot b = a;"
212 write
"\t\t\tif (a.Id() > 0) a = new {tagc}{c}<Root>();"
214 write
"\tTest(b, b, 10, -100);"
215 write
"\tTest(a, a, {loops}, 0);"
218 write
"static void Test(Root a, Root b, int loops, int start) \{"
219 write
"\tint x = start;"
220 write
"\tfor(int i = 0; i < loops; i++) \{"
221 write
"\t\tfor(int j = 0; j < loops; j++) \{"
223 if dry
then test
= "" else test
= "a is {classes[middle]}<Root> && "
224 write
"\t\t\tif({test}x>=0) \{"
225 if check
then write
"\t\t\t\tx++;"
226 write
"\} else \{ x--; a = b; \};"
229 write
"\tSystem.Console.WriteLine(x);"
235 fun writescala
(dir
: String, name
: String, interfaces
: Bool)
239 file
= new OFStream.open
("{dir}/{name}.scala")
242 write
"object {name} \{"
243 write
"class Root\n\t\{ def id: Int = 0 \}"
246 write
"trait {c}[E] "
248 write
"class {c}[E] "
250 if c
.supers
.is_empty
then
251 write
"\textends Root"
252 else for s
in [c
.supers
.first
] do
253 write
"\textends {s}[E]"
257 write
"class X{c}[E] extends {c}[E]"
260 write
"\toverride def id: Int = {c.id}"
264 write
"def main(args: Array[String]) = \{"
266 if interfaces
then tagc
= "X"
267 write
"\tvar a:Root = new {tagc}{classes.first}[Root]()"
268 write
"\tvar b:Root = a"
270 write
"\t\t\tif (a.id > 0) a = new {tagc}{c}[Root]();"
272 write
"\ttest(b, b, 10, -100)"
273 write
"\ttest(a, a, {loops}, 0)"
276 write
"def test(aa:Root, b:Root, l: Int, start: Int) = \{"
278 write
"\tvar x = start"
279 write
"\tvar loops = l"
281 write
"\twhile (i < loops) \{"
282 write
"\t\tvar j = 0"
283 write
"\t\twhile (j < loops) \{"
285 if dry
then test
= "" else test
= "a.isInstanceOf[{classes[middle]}[Root]] && "
286 write
"\t\tif ({test}x>=0) \{"
287 if check
then write
"\t\t\tx += 1"
288 write
"\} else \{ x = x - 1; a = b; \}"
293 write
"\t\t\tprintln(x)"
300 fun writecpp
(dir
: String, name
: String)
304 file
= new OFStream.open
("{dir}/{name}.cpp")
306 write
"#include <iostream>"
307 write
"#include <stdlib.h>"
308 write
"class Root\n\t\{ public: virtual int id() \{ return 0;\} \};"
310 write
"template<class E>"
312 if c
.supers
.is_empty
then
313 write
"\t: public virtual Root"
314 else for s
in [c
.supers
.first
] do
315 write
"\t: public virtual {s}<E>"
318 write
"\tpublic: virtual int id() \{ return {c.id}; \}"
322 write
"void test(Root *a, Root *b, int loops, int start) \{"
323 write
"\tint x = start;"
324 write
"\tfor(int i = 0; i < loops; i++) \{"
325 write
"\t\tfor(int j = 0; j < loops; j++) \{"
327 if dry
then test
= "" else
328 write
"\t\t\t{classes[middle]}<Root> *to = dynamic_cast<{classes[middle]}<Root>*>(a);"
331 write
"\t\tif({test}x>=0) \{"
332 if check
then write
"\t\t\tx += 1"
333 write
"\} else \{ x--; a = b;\}"
336 write
"\tstd::cout << x << std::endl;"
339 write
"int main(int argc, char **argv) \{"
340 write
"\tRoot *a = new {classes.first}<Root>();"
341 write
"\tRoot *b = a;"
343 write
"\t\t\tif (a->id() > 0) a = new {c}<Root>();"
345 write
"\ttest(b, b, 10, -100);"
346 write
"\ttest(a, a, {loops}, 0);"
352 fun writee
(dir
: String, name
: String, se
: Bool)
355 dir
= "{dir}/se/{name}"
357 dir
= "{dir}/es/{name}"
360 file
= new OFStream.open
("{dir}/root.e")
363 if se
then istk
= " is"
365 write
"feature id: INTEGER {istk} do Result := 0 end"
370 file
= new OFStream.open
("{dir}/{c}.e")
371 write
"class {c}[E] "
372 if c
.supers
.is_empty
then
373 write
"\tinherit ROOT"
374 else for s
in [c
.supers
.first
] do
375 write
"\tinherit {s}[E]"
377 write
"\t\tredefine id end"
379 write
"\tid: INTEGER {istk} do Result := {c.id} end"
384 file
= new OFStream.open
("{dir}/app{name}.e")
385 write
"class APP{name.to_upper}"
387 write
"insert ARGUMENTS"
395 write
"\tmake(args: ARRAY[STRING]){istk}"
398 write
"\t\t\ta: ROOT"
399 write
"\t\t\tb: ROOT"
401 write
"\t\t\tcreate \{{classes.first}[ROOT]\} a"
404 write
"\t\t\tif a.id > 0 then create \{{c}[ROOT]\} a end"
406 write
"\t\t\ttest(b, b, 10, -100)"
407 write
"\t\t\ttest(a, a, {loops}, 0)"
410 write
"\ttest(a: ROOT; b: ROOT; l: INTEGER; start: INTEGER){istk}"
412 write
"\t\t\to: ROOT"
413 write
"\t\t\tto: {classes[middle]}[ROOT]"
414 write
"\t\t\ti: INTEGER"
415 write
"\t\t\tj: INTEGER"
416 write
"\t\t\tx: INTEGER"
417 write
"\t\t\tloops: INTEGER"
420 write
"\t\t\tx := start"
421 write
"\t\t\tloops := l"
422 write
"\t\t\tfrom i := 0 until i>=loops loop"
423 write
"\t\t\t\tfrom j := 0 until j>=loops loop"
428 write
"\t\t\t\t\tto ?= o"
429 test
= "to /= Void and then "
431 write
"\t\t\t\t\tif {test}x >= 0 then"
432 if check
then write
"\t\t\t\t\tx := x + 1"
433 write
"\t\t\t\t\telse x := x - 1; o := b end"
434 write
"\t\t\t\t\tj := j + 1"
436 write
"\t\t\t\ti := i + 1"
438 write
"\t\t\tprint(x.out)"
447 var g
= new Generator
448 var outdir
= args
.first
450 var use_interfaces
= true
451 if args
.length
> 2 then g
.dept
= args
[2].to_i
452 if args
.length
> 3 then use_interfaces
= false
456 g
.writenit
(outdir
, name
)
457 g
.writejava
(outdir
, name
, use_interfaces
)
458 g
.writecsharp
(outdir
, name
, use_interfaces
)
459 g
.writescala
(outdir
, name
, use_interfaces
)
460 g
.writecpp
(outdir
, name
)
461 g
.writee
(outdir
, "{name}_se", true)
462 g
.writee
(outdir
, name
, false)