benchs/strings: Update string basic functions benchmarks
[nit.git] / benchmarks / strings / chain_concat.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 measuring the performance of several concatenations on Text variants
12 module chain_concat
13
14 import bench_base
15 import opts
16
17 private fun bench_string(str_size: Int, nb_ccts: Int, loops: Int)
18 do
19 var lft = prepare_string(str_size)
20 for i in [0 .. loops[ do
21 var str = ""
22 for j in [0 .. nb_ccts[ do
23 str += lft
24 end
25 end
26 end
27
28 private fun bench_buffer(str_size: Int, nb_ccts: Int, loops: Int)
29 do
30 var lft = prepare_string(str_size)
31 for i in [0 .. loops[ do
32 var buf = new Buffer
33 for j in [0 .. nb_ccts[ do
34 buf.append(lft)
35 end
36 end
37 end
38
39 var opts = new OptionContext
40 var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
41 var nb_ccts = new OptionInt("Number of concatenations per loop", -1, "--ccts")
42 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
43 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
44 opts.add_option(mode, nb_ccts, loops, strlen)
45
46 opts.parse(args)
47
48 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
49 opts.usage
50 exit -1
51 end
52
53 var modval = mode.value
54
55 if modval == 0 then
56 bench_string(strlen.value, nb_ccts.value, loops.value)
57 else if modval == 1 then
58 bench_buffer(strlen.value, nb_ccts.value, loops.value)
59 else
60 opts.usage
61 exit -1
62 end