benchmarks/strings: Adapted for new Ropes.
[nit.git] / benchmarks / strings / iteration_bench.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Benches for iteration on variants of Text
12 module iteration_bench
13
14 import opts
15
16 fun bench_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int)
17 do
18 var a = "a" * strlen
19 var x = a
20 for i in [0 .. nb_cct] do x += a
21 var cnt = 0
22 var c: Char
23 while cnt != loops do
24 for i in x do
25 c = i
26 end
27 cnt += 1
28 end
29 end
30
31 fun bench_flatstr_index(nb_cct: Int, loops: Int, strlen: Int)
32 do
33 var a = "a" * strlen
34 var x = a
35 for i in [0 .. nb_cct] do x += a
36 var cnt = 0
37 var c: Char
38 var pos = 0
39 while cnt != loops do
40 pos = 0
41 while pos < x.length do
42 c = x[pos]
43 pos += 1
44 end
45 cnt += 1
46 end
47 end
48
49 fun bench_flatbuf_iter(nb_cct: Int, loops: Int, strlen: Int)
50 do
51 var a = "a" * strlen
52 var x = new FlatBuffer.from(a)
53 for i in [0 .. nb_cct] do x.append a
54 var cnt = 0
55 var c: Char
56 while cnt != loops do
57 for i in x do
58 c = i
59 end
60 cnt += 1
61 end
62 end
63
64 fun bench_flatbuf_index(nb_cct: Int, loops: Int, strlen: Int)
65 do
66 var a = "a" * strlen
67 var x = new FlatBuffer.from(a)
68 for i in [0 .. nb_cct] do x.append a
69 var cnt = 0
70 var c: Char
71 var pos = 0
72 while cnt != loops do
73 pos = 0
74 while pos < x.length do
75 c = x[pos]
76 pos += 1
77 end
78 cnt += 1
79 end
80 end
81
82 var opts = new OptionContext
83 var mode = new OptionEnum(["flatstr", "flatbuf"], "Mode", -1, "-m")
84 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
85 var nb_ccts = new OptionInt("Number of concatenations done to the string (in the case of the rope, this will increase its depth)", -1, "--ccts")
86 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
87 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
88 opts.add_option(mode, nb_ccts, loops, strlen, access_mode)
89
90 opts.parse(args)
91
92 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
93 opts.usage
94 exit(-1)
95 end
96
97 var modval = mode.value
98 var iterval = access_mode.value
99
100 if modval == 0 then
101 if iterval == 0 then
102 bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value)
103 else if iterval == 1 then
104 bench_flatstr_index(nb_ccts.value, loops.value, strlen.value)
105 else
106 opts.usage
107 exit(-1)
108 end
109 else if modval == 1 then
110 if iterval == 0 then
111 bench_flatbuf_iter(nb_ccts.value, loops.value, strlen.value)
112 else if iterval == 1 then
113 bench_flatbuf_index(nb_ccts.value, loops.value, strlen.value)
114 else
115 opts.usage
116 exit(-1)
117 end
118 else
119 opts.usage
120 exit(-1)
121 end