benchmarks/string: Updated bench scripts for strings
[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 intrude import standard::ropes
16
17 redef class Concat
18 redef fun +(o) do
19 var s = o.to_s
20 return new Concat(self, s)
21 end
22 end
23
24 redef class FlatString
25 redef fun +(o) do
26 var s = o.to_s
27 var b = new FlatBuffer.with_capacity(length + s.length)
28 b.append self
29 for i in s.substrings do b.append i
30 return b.to_s
31 end
32 end
33
34 fun bench_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int)
35 do
36 var a = "a" * strlen
37 a = a * nb_cct
38 var cnt = 0
39 var c: Char
40 while cnt != loops do
41 for i in a do
42 c = i
43 end
44 cnt += 1
45 end
46 end
47
48 fun bench_flatstr_index(nb_cct: Int, loops: Int, strlen: Int)
49 do
50 var a = "a" * strlen
51 a = a * nb_cct
52 var cnt = 0
53 var c: Char
54 var pos = 0
55 while cnt != loops do
56 pos = 0
57 while pos < a.length do
58 c = a[pos]
59 pos += 1
60 end
61 cnt += 1
62 end
63 end
64
65 fun bench_ropestr_iter(nb_cct: Int, loops: Int, strlen: Int)
66 do
67 var a = "a" * strlen
68 var x: String = new Concat(a, a)
69 for i in [2 .. nb_cct[ do x = new Concat(x, a)
70 var cnt = 0
71 var c: Char
72 while cnt != loops do
73 for i in x do
74 c = i
75 end
76 cnt += 1
77 end
78 end
79
80 fun bench_ropestr_index(nb_cct: Int, loops: Int, strlen: Int)
81 do
82 var a = "a" * strlen
83 var x: String = new Concat(a, a)
84 for i in [2 .. nb_cct[ do x = new Concat(x, a)
85 var cnt = 0
86 var c: Char
87 var pos = 0
88 while cnt != loops do
89 pos = 0
90 while pos < x.length do
91 c = x[pos]
92 pos += 1
93 end
94 cnt += 1
95 end
96 end
97
98 fun bench_flatbuf_iter(nb_cct: Int, loops: Int, strlen: Int)
99 do
100 var a = "a" * strlen
101 a = a * nb_cct
102 var x = new FlatBuffer.from(a)
103 var cnt = 0
104 var c: Char
105 while cnt != loops do
106 for i in x do
107 c = i
108 end
109 cnt += 1
110 end
111 end
112
113 fun bench_flatbuf_index(nb_cct: Int, loops: Int, strlen: Int)
114 do
115 var a = "a" * strlen
116 a = a * nb_cct
117 var x = new FlatBuffer.from(a)
118 var cnt = 0
119 var c: Char
120 var pos = 0
121 while cnt != loops do
122 pos = 0
123 while pos < x.length do
124 c = x[pos]
125 pos += 1
126 end
127 cnt += 1
128 end
129 end
130
131 fun bench_ropebuf_iter(nb_cct: Int, loops: Int, strlen: Int)
132 do
133 var a = "a" * strlen
134 var x = new RopeBuffer.from(a)
135 for i in [0 .. nb_cct[ do x.append a
136 var cnt = 0
137 var c: Char
138 while cnt != loops do
139 for i in x do
140 c = i
141 end
142 cnt += 1
143 end
144 end
145
146 fun bench_ropebuf_index(nb_cct: Int, loops: Int, strlen: Int)
147 do
148 var a = "a" * strlen
149 var x = new RopeBuffer.from(a)
150 for i in [0 .. nb_cct[ do x.append a
151 var cnt = 0
152 var c: Char
153 var pos = 0
154 while cnt != loops do
155 pos = 0
156 while pos < x.length do
157 c = x[pos]
158 pos += 1
159 end
160 cnt += 1
161 end
162 end
163
164 var opts = new OptionContext
165 var mode = new OptionEnum(["flatstr", "flatbuf", "ropestr", "ropebuf"], "Mode", -1, "-m")
166 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
167 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")
168 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
169 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
170 opts.add_option(mode, nb_ccts, loops, strlen, access_mode)
171
172 opts.parse(args)
173
174 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
175 opts.usage
176 exit(-1)
177 end
178
179 var modval = mode.value
180 var iterval = access_mode.value
181
182 if modval == 0 then
183 if iterval == 0 then
184 bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value)
185 else if iterval == 1 then
186 bench_flatstr_index(nb_ccts.value, loops.value, strlen.value)
187 else
188 opts.usage
189 exit(-1)
190 end
191 else if modval == 1 then
192 if iterval == 0 then
193 bench_flatbuf_iter(nb_ccts.value, loops.value, strlen.value)
194 else if iterval == 1 then
195 bench_flatbuf_index(nb_ccts.value, loops.value, strlen.value)
196 else
197 opts.usage
198 exit(-1)
199 end
200 else if modval == 2 then
201 if iterval == 0 then
202 bench_ropestr_iter(nb_ccts.value, loops.value, strlen.value)
203 else if iterval == 1 then
204 bench_ropestr_index(nb_ccts.value, loops.value, strlen.value)
205 else
206 opts.usage
207 exit(-1)
208 end
209 else if modval == 3 then
210 if iterval == 0 then
211 bench_ropebuf_iter(nb_ccts.value, loops.value, strlen.value)
212 else if iterval == 1 then
213 bench_ropebuf_index(nb_ccts.value, loops.value, strlen.value)
214 else
215 opts.usage
216 exit(-1)
217 end
218 else
219 opts.usage
220 exit(-1)
221 end