35466f9be28623a4ed07377c1dfac60623ea718f
[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 opts
15
16 fun bench_flatstr(str_size: Int, nb_ccts: Int, loops: Int)
17 do
18 var lft = "a" * str_size
19
20 for i in [0..loops] do
21 var str: String = lft
22 for j in [0..nb_ccts] do
23 str += lft
24 end
25 end
26 end
27
28 fun bench_rope(str_size: Int, nb_ccts: Int, loops: Int)
29 do
30 var lft = "a" * str_size
31
32 for i in [0..loops] do
33 var str: String = new RopeString.from(lft)
34 for j in [0..nb_ccts] do
35 str += lft
36 end
37 end
38 end
39
40 fun bench_flatbuf(str_size: Int, nb_ccts: Int, loops: Int)
41 do
42 var lft = "a" * str_size
43
44 for i in [0..loops] do
45 var buf = new FlatBuffer.from(lft)
46 for j in [0..nb_ccts] do
47 buf.append(lft)
48 end
49 buf.to_s
50 end
51 end
52
53 var opts = new OptionContext
54 var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "Mode", -1, "-m")
55 var nb_ccts = new OptionInt("Number of concatenations per loop", -1, "--ccts")
56 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
57 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
58 opts.add_option(mode, nb_ccts, loops, strlen)
59
60 opts.parse(args)
61
62 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
63 opts.usage
64 exit -1
65 end
66
67 var modval = mode.value
68
69 if modval == 0 then
70 bench_rope(strlen.value, nb_ccts.value, loops.value)
71 else if modval == 1 then
72 bench_flatstr(strlen.value, nb_ccts.value, loops.value)
73 else if modval == 2 then
74 bench_flatbuf(strlen.value, nb_ccts.value, loops.value)
75 else
76 opts.usage
77 exit -1
78 end