811edddf53763a9a2919b71d46f0d72a038887a6
[nit.git] / benchmarks / bench_strings.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 source ./bench_common.sh
17 source ./bench_plot.sh
18
19 # Default number of times a command must be run with bench_command
20 # Can be overrided with 'the option -n'
21 count=2
22
23 function usage()
24 {
25 echo "run_bench: [options]* bench_name args"
26 echo " -v: verbose mode"
27 echo " -n count: number of execution for each bar (default: $count)"
28 echo " -h: this help"
29 echo ""
30 echo "Benches : "
31 echo " all : all benches"
32 echo " - usage : * max_nb_cct loops strlen"
33 echo " iter: bench iterations"
34 echo " - usage : iter max_nb_cct loops strlen"
35 echo " cct: concatenation benching"
36 echo " - usage : cct max_nb_cct loops strlen"
37 echo " substr: substring benching"
38 echo " - usage : substr max_nb_cct loops strlen"
39 }
40
41 function benches()
42 {
43 bench_concat $@;
44 bench_iteration $@;
45 bench_substr $@;
46 }
47
48 function bench_concat()
49 {
50 if $verbose; then
51 echo "*** Benching concat performance ***"
52 fi
53
54 prepare_res concat_ropes.out concat_ropes ropes
55 if $verbose; then
56 echo "Ropes :"
57 fi
58 for i in `seq 1 "$1"`; do
59 if $verbose; then
60 echo "String length = $i, Concats/loop = $2, Loops = $3"
61 fi
62 bench_command $i ropes$i ./chain_concat -m rope --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
63 done
64
65 prepare_res concat_flat.out concat_flat flatstring
66 if $verbose; then
67 echo "FlatStrings :"
68 fi
69 for i in `seq 1 "$1"`; do
70 if $verbose; then
71 echo "String length = $i, Concats/loop = $2, Loops = $3"
72 fi
73 bench_command $i flatstring$i ./chain_concat -m flatstr --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
74 done
75
76 prepare_res concat_buf.out concat_buf flatbuffer
77 if $verbose; then
78 echo "FlatBuffers :"
79 fi
80 for i in `seq 1 "$1"`; do
81 if $verbose; then
82 echo "String length = $i, Concats/loop = $2, Loops = $3"
83 fi
84 bench_command $i flatbuffer$i ./chain_concat -m flatbuf --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
85 done
86
87 plot concat.gnu
88 }
89
90 function bench_iteration()
91 {
92 if $verbose; then
93 echo "*** Benching iteration performance ***"
94 fi
95
96 prepare_res iter_ropes_iter.out iter_ropes_iter ropes_iter
97 if $verbose; then
98 echo "Ropes by iterator :"
99 fi
100 for i in `seq 1 "$1"`; do
101 if $verbose; then
102 echo "String base length = $1, Concats (depth of the rope) = $i, Loops = $3"
103 fi
104 bench_command $i ropes_iter$i ./iteration_bench -m rope --iter-mode iterator --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
105 done
106
107 prepare_res iter_ropes_index.out iter_ropes_index ropes_index
108 if $verbose; then
109 echo "Ropes by index :"
110 fi
111 for i in `seq 1 "$1"`; do
112 if $verbose; then
113 echo "String base length = $1, Concats (depth of the rope) = $i, Loops = $3"
114 fi
115 bench_command $i ropes_index$i ./iteration_bench -m rope --iter-mode index --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
116 done
117
118 prepare_res iter_flat_iter.out iter_flat_iter flatstring_iter
119 if $verbose; then
120 echo "FlatStrings by iterator :"
121 fi
122 for i in `seq 1 "$1"`; do
123 if $verbose; then
124 echo "String base length = $1, Concats = $i, Loops = $3"
125 fi
126 bench_command $i flatstr_iter$i ./iteration_bench -m flatstr --iter-mode iterator --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
127 done
128
129 prepare_res iter_flat_index.out iter_flat_index flatstring_index
130 if $verbose; then
131 echo "FlatStrings by index :"
132 fi
133 for i in `seq 1 "$1"`; do
134 if $verbose; then
135 echo "String base length = $1, Concats = $i, Loops = $3"
136 fi
137 bench_command $i flatstr_index$i ./iteration_bench -m flatstr --iter-mode index --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
138 done
139
140 prepare_res iter_buf_iter.out iter_buf_iter flatbuffer_iter
141 if $verbose; then
142 echo "FlatBuffers by iterator :"
143 fi
144 for i in `seq 1 "$1"`; do
145 if $verbose; then
146 echo "String base length = $1, Concats = $i, Loops = $3"
147 fi
148 bench_command $i flatbuf_iter$i ./iteration_bench -m flatbuf --iter-mode iterator --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
149 done
150
151 prepare_res iter_buf_index.out iter_buf_index flatbuffer_index
152 if $verbose; then
153 echo "FlatBuffers by index:"
154 fi
155 for i in `seq 1 "$1"`; do
156 if $verbose; then
157 echo "String base length = $1, Concats = $i, Loops = $3"
158 fi
159 bench_command $i flatbuf_index$i ./iteration_bench -m flatbuf --iter-mode index --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
160 done
161
162 plot iter.gnu
163 }
164
165 function bench_substr()
166 {
167 if $verbose; then
168 echo "*** Benching substring performance ***"
169 fi
170
171 prepare_res substr_ropes.out substr_ropes ropes
172 if $verbose; then
173 echo "Ropes :"
174 fi
175 for i in `seq 1 "$1"`; do
176 if $verbose; then
177 echo "String length = $i, loops = $2, Loops = $3"
178 fi
179 bench_command $i ropes$i ./substr_bench -m rope --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
180 done
181
182 prepare_res substr_flat.out substr_flat flatstring
183 if $verbose; then
184 echo "FlatStrings :"
185 fi
186 for i in `seq 1 "$1"`; do
187 if $verbose; then
188 echo "String length = $i, loops = $2, Loops = $3"
189 fi
190 bench_command $i flatstring$i ./substr_bench -m flatstr --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
191 done
192
193 prepare_res substr_buf.out substr_buf flatbuffer
194 if $verbose; then
195 echo "FlatBuffers :"
196 fi
197 for i in `seq 1 "$1"`; do
198 if $verbose; then
199 echo "String length = $i, loops = $2, Loops = $3"
200 fi
201 bench_command $i flatbuffer$i ./substr_bench -m flatbuf --loops $2 --strlen $3 --ccts $i "NIT_GC_CHOOSER=large"
202 done
203
204 plot substr.gnu
205 }
206
207 stop=false
208 while [ "$stop" = false ]; do
209 case "$1" in
210 -v) verbose=true; shift;;
211 -h) usage; exit;;
212 -n) count="$2"; shift; shift;;
213 *) stop=true
214 esac
215 done
216
217 if test $# -ne 4; then
218 usage
219 exit
220 fi
221
222 if $verbose; then
223 echo "Compiling"
224 fi
225
226 ../bin/nitg --global ./strings/chain_concat.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\""
227 ../bin/nitg --global ./strings/iteration_bench.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\""
228 ../bin/nitg --global ./strings/substr_bench.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\""
229
230 case "$1" in
231 iter) shift; bench_iteration $@ ;;
232 cct) shift; bench_concat $@ ;;
233 substr) shift; bench_substr $@ ;;
234 all) shift; benches $@ ;;
235 *) usage; exit;;
236 esac