lib/standard/stream: Renamed streams for more explicit denomination
[nit.git] / benchmarks / languages / bench_base.nit
1 #!/usr/bin/env nit
2
3 # Microbenchmak generation for multiple language
4 # Just a quick an dirty Nit script file :)
5
6 # This benchmark focuses on effects of the hierarchy dept
7 # on the typetest performances
8
9 # class Root
10 # class Klass[E] super Root
11 # class C1...CX super Root
12 # Klass[CX] isa Klass[C1]
13
14 class Klass
15 var id: Int
16 var supers = new Array[Klass]
17 var all_supers = new HashSet[Klass]
18 redef fun to_s
19 do
20 return "C{id}"
21 end
22 end
23
24 class Generator
25
26 var classes = new Array[Klass]
27
28 var dept writable = 5
29 var loops writable = 50000
30 var middle writable = 0
31 var dry writable = false
32 var check writable = false
33
34 fun genhier
35 do
36 var s: nullable Klass = null
37 for d in [1..dept] do
38 var c = new Klass(d)
39
40 classes.add(c)
41 c.all_supers.add(c)
42 if s != null then
43 c.supers.add(s)
44 c.all_supers.add_all(s.all_supers)
45 end
46 s = c
47 end
48 middle = (dept + 1) / 2
49 end
50
51 var file: nullable FileWriter = null
52 fun write(str: String)
53 do
54 file.write(str)
55 file.write("\n")
56 end
57
58 fun initnit(res: Array[String]) is abstract
59 fun testnit: String do return "true"
60
61 fun writenit(dir: String, name: String)
62 do
63 dir = "{dir}/nit"
64 dir.mkdir
65 file = new FileWriter.open("{dir}/{name}.nit")
66
67 write "class Root\n\tfun id: Int do return 0\nend"
68 for c in classes do
69 write "class {c}[E]"
70 if c.supers.is_empty then
71 write "\tsuper Root"
72 else for s in c.supers do
73 write "\tsuper {s}[E]"
74 end
75 write "\tredef fun id do return {c.id}"
76 write "end"
77 end
78
79 write "fun test(a,b: Root, loops, start: Int)"
80 write "do"
81 write "var x = start"
82 write "var i = 0"
83 write "while i < loops do"
84 write "\tvar j = 0"
85 write "\twhile j < loops do"
86 var test = "true"
87 if not dry then test = testnit
88 write "\t\tif {test} and x >= 0 then"
89 if check then write "\t\tx += 1"
90 write "\telse"
91 write "\t\t\tx = x - 1 + i - j"
92 write "\t\t\ta = b"
93 write "\t\tend"
94 write "\t\tj += 1"
95 write "\tend"
96 write "\ti += 1"
97 write "end"
98 write "print x"
99 write "end"
100
101 var ia = new Array[String]
102 initnit(ia)
103 write "var a: Root = {ia.first}"
104 write "var b: Root = a"
105 for i in ia do
106 write "\t\t\tif a.id > 0 then a = {i}"
107 end
108 write "test(b, b, 10, -100)"
109 write "test(a, a, {loops}, 0)"
110
111 file.close
112 end
113
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)
117 do
118 dir = "{dir}/java"
119 dir.mkdir
120 file = new FileWriter.open("{dir}/{name}.java")
121
122 var cl = ""
123 if interfaces then cl = "X"
124 write "class {name} \{"
125 if interfaces then
126 write "static interface Root\n\t\{ int id(); \}"
127 else
128 write "static class Root\n\t\{ int id() \{ return 0;\} \}"
129 end
130 for c in classes do
131 if interfaces then
132 write "static interface {c}<E> "
133 else
134 write "static class {c}<E> "
135 end
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>"
140 end
141 if interfaces then
142 write "\{\}"
143 write "static class X{c}<E> implements {c}<E>"
144 end
145 write "\{"
146 write "\tpublic int id() \{ return {c.id}; \}"
147 write "\}"
148 end
149
150 write "static public void main(String args[]) \{"
151 var ia = new Array[String]
152 initjava(ia, interfaces)
153 write "Root a = {ia.first};"
154 write "Root b = a;"
155 for i in ia do
156 write "\t\t\tif (a.id() > 0) a = {i};"
157 end
158 write "\ttest(b, b, 10, -100);"
159 write "\ttest(a, a, {loops}, 0);"
160 write "\}"
161
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++) \{"
166 var test = "true"
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;\}"
172 write "\t\t}"
173 write "\t\}"
174 write "\tSystem.out.println(x);"
175 write "\}"
176 write "\}"
177 file.close
178 end
179
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)
183 do
184 dir = "{dir}/cs"
185 dir.mkdir
186 file = new FileWriter.open("{dir}/{name}.cs")
187
188 var cl = ""
189 if interfaces then cl = "X"
190 write "class {name} \{"
191 if interfaces then
192 write "interface Root\n\t\{ int Id(); \}"
193 else
194 write "class Root\n\t\{ public int Id() \{ return 0;\} \}"
195 end
196 for c in classes do
197 if interfaces then
198 write "interface {c}<out E> "
199 else
200 write "class {c}<E> "
201 end
202 if c.supers.is_empty then
203 write "\t: Root"
204 else for s in [c.supers.first] do
205 write "\t: {s}<E>"
206 end
207 if interfaces then
208 write "\{\}"
209 write "class X{c}<E> : {c}<E>"
210 end
211 write "\{"
212 write "\tpublic int Id() \{ return {c.id}; \}"
213 write "\}"
214 end
215
216 write "static void Main(string[] args) \{"
217 var ia = new Array[String]
218 initcsharp(ia, interfaces)
219 write "Root a = {ia.first};"
220 write "Root b = a;"
221 for i in ia do
222 write "\t\t\tif (a.Id() > 0) a = {i};"
223 end
224 write "\tTest(b, b, 10, -100);"
225 write "\tTest(a, a, {loops}, 0);"
226 write "\}"
227
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++) \{"
232 var test = "true"
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; \};"
237 write "\t\t}"
238 write "\t\}"
239 write "\tSystem.Console.WriteLine(x);"
240 write "\}"
241 write "\}"
242 file.close
243 end
244
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)
248 do
249 dir = "{dir}/scala"
250 dir.mkdir
251 file = new FileWriter.open("{dir}/{name}.scala")
252
253 var cl = ""
254 write "object {name} \{"
255 write "class Root\n\t\{ def id: Int = 0 \}"
256 for c in classes do
257 if interfaces then
258 write "trait {c}[+E] "
259 else
260 write "class {c}[+E] "
261 end
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]"
266 end
267 if interfaces then
268 write "\{\}"
269 write "class X{c}[E] extends {c}[E]"
270 end
271 write "\{"
272 write "\toverride def id: Int = {c.id}"
273 write "\}"
274 end
275
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;"
281 for i in ia do
282 write "\t\t\tif (a.id > 0) a = {i};"
283 end
284 write "\ttest(b, b, 10, -100)"
285 write "\ttest(a, a, {loops}, 0)"
286 write "\}"
287
288 write "def test(aa:Root, b:Root, l: Int, start: Int) = \{"
289 write "\tvar a = aa"
290 write "\tvar x = start"
291 write "\tvar loops = l"
292 write "\tvar i = 0"
293 write "\twhile (i < loops) \{"
294 write "\t\tvar j = 0"
295 write "\t\twhile (j < loops) \{"
296 var test = "true"
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; \}"
302 write "\t\tj += 1"
303 write "\t\t\}"
304 write "\ti += 1"
305 write "\t\}"
306 write "\t\t\tprintln(x)"
307 write "\}"
308 write "\}"
309
310 file.close
311 end
312
313 fun initcpp(res: Array[String]) is abstract
314 fun testcpp: String do return "true"
315 fun writecpp(dir: String, name: String)
316 do
317 dir = "{dir}/cpp"
318 dir.mkdir
319 file = new FileWriter.open("{dir}/{name}.cpp")
320
321 write "#include <iostream>"
322 write "#include <stdlib.h>"
323 write "class Root\n\t\{ public: virtual int id() \{ return 0;\} \};"
324 for c in classes do
325 write "template<class E>"
326 write "class {c} "
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>"
331 end
332 write "\{"
333 write "\tpublic: virtual int id() \{ return {c.id}; \}"
334 write "\};"
335 end
336
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++) \{"
341 var test = "true"
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;\}"
346 write "\t\t}"
347 write "\t\}"
348 write "\tstd::cout << x << std::endl;"
349 write "\}"
350
351 write "int main(int argc, char **argv) \{"
352 var ia = new Array[String]
353 initcpp(ia)
354 write "Root *a = {ia.first};"
355 write "Root *b = a;"
356 for i in ia do
357 write "\t\t\tif (a->id() > 0) a = {i};"
358 end
359 write "\ttest(b, b, 10, -100);"
360 write "\ttest(a, a, {loops}, 0);"
361 write "\}"
362
363 file.close
364 end
365
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)
370 do
371 if se then
372 dir = "{dir}/se/{name}"
373 else
374 dir = "{dir}/es/{name}"
375 end
376 dir.mkdir
377 file = new FileWriter.open("{dir}/root.e")
378
379 var istk = ""
380 if se then istk = " is"
381 write "class ROOT"
382 write "feature id: INTEGER {istk} do Result := 0 end"
383 write "end"
384 file.close
385
386 for c in classes do
387 file = new FileWriter.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]"
393 end
394 write "\t\tredefine id end"
395 write "feature"
396 write "\tid: INTEGER {istk} do Result := {c.id} end"
397 write "end"
398 file.close
399 end
400
401 file = new FileWriter.open("{dir}/app{name}.e")
402 write "class APP{name.to_upper}"
403 if se then
404 write "insert ARGUMENTS"
405 end
406 write "create make"
407 write "feature"
408
409 if se then
410 write "\tmake{istk}"
411 else
412 write "\tmake(args: ARRAY[STRING]){istk}"
413 end
414 write "\t\tlocal"
415 write "\t\t\ta: ROOT"
416 write "\t\t\tb: ROOT"
417 write "\t\tdo"
418 var ia = new Array[String]
419 inite(ia,se)
420 write "{ia.first}"
421 write "b := a"
422 for i in ia do
423 write "\t\t\tif a.id > 0 then {i} end"
424 end
425 write "\t\t\ttest(b, b, 10, -100)"
426 write "\t\t\ttest(a, a, {loops}, 0)"
427 write "\t\tend"
428
429 write "\ttest(aa: ROOT; b: ROOT; l: INTEGER; start: INTEGER){istk}"
430 write "\t\tlocal"
431 write "\t\t\ta: ROOT"
432 write "\t\t\ti: INTEGER"
433 write "\t\t\tj: INTEGER"
434 write "\t\t\tx: INTEGER"
435 locale(se)
436 write "\t\t\tloops: INTEGER"
437 write "\t\tdo"
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"
443 var test = "True"
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"
449 write "\t\t\t\tend"
450 write "\t\t\t\ti := i + 1"
451 write "\t\t\tend"
452 write "\t\t\tprint(x.out)"
453 write "\t\t\tprint(\"%N\")"
454 write "\t\tend"
455 write "end"
456 file.close
457 end
458
459 var count = false
460
461 fun genall
462 do
463 var g = self
464 var outdir = args.first
465 var name = args[1]
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
469
470 g.genhier
471
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)
479 end
480 end
481
482 var g = new Generator
483 g.genall