a8047d284066b418c2bd3227ab92796feefaa1cd
[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_rope_iter(nb_cct: Int, loops: Int, strlen: Int)
17 do
18 var a = "a" * strlen
19 var x:String = new RopeString.from(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_rope_index(nb_cct: Int, loops: Int, strlen: Int)
32 do
33 var a = "a" * strlen
34 var x:String = new RopeString.from(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_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int)
50 do
51 var a = "a" * strlen
52 var x = a
53 for i in [0 .. nb_cct] do x += 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_flatstr_index(nb_cct: Int, loops: Int, strlen: Int)
65 do
66 var a = "a" * strlen
67 var x = a
68 for i in [0 .. nb_cct] do x += 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 fun bench_flatbuf_iter(nb_cct: Int, loops: Int, strlen: Int)
83 do
84 var a = "a" * strlen
85 var x = new FlatBuffer.from(a)
86 for i in [0 .. nb_cct] do x.append a
87 var cnt = 0
88 var c: Char
89 while cnt != loops do
90 for i in x do
91 c = i
92 end
93 cnt += 1
94 end
95 end
96
97 fun bench_flatbuf_index(nb_cct: Int, loops: Int, strlen: Int)
98 do
99 var a = "a" * strlen
100 var x = new FlatBuffer.from(a)
101 for i in [0 .. nb_cct] do x.append a
102 var cnt = 0
103 var c: Char
104 var pos = 0
105 while cnt != loops do
106 pos = 0
107 while pos < x.length do
108 c = x[pos]
109 pos += 1
110 end
111 cnt += 1
112 end
113 end
114
115 var opts = new OptionContext
116 var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "Mode", -1, "-m")
117 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
118 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")
119 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
120 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
121 opts.add_option(mode, nb_ccts, loops, strlen, access_mode)
122
123 opts.parse(args)
124
125 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
126 opts.usage
127 exit(-1)
128 end
129
130 var modval = mode.value
131 var iterval = access_mode.value
132
133 if modval == 0 then
134 if iterval == 0 then
135 bench_rope_iter(nb_ccts.value, loops.value, strlen.value)
136 else if iterval == 1 then
137 bench_rope_index(nb_ccts.value, loops.value, strlen.value)
138 else
139 opts.usage
140 exit(-1)
141 end
142 else if modval == 1 then
143 if iterval == 0 then
144 bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value)
145 else if iterval == 1 then
146 bench_flatstr_index(nb_ccts.value, loops.value, strlen.value)
147 else
148 opts.usage
149 exit(-1)
150 end
151 else if modval == 2 then
152 if iterval == 0 then
153 bench_flatbuf_iter(nb_ccts.value, loops.value, strlen.value)
154 else if iterval == 1 then
155 bench_flatbuf_index(nb_ccts.value, loops.value, strlen.value)
156 else
157 opts.usage
158 exit(-1)
159 end
160 else
161 opts.usage
162 exit(-1)
163 end