5021e6629c646d7babaa3ff913e2f6687625324c
[nit.git] / benchmarks / strings / substr_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 on the substring operation on variants of Text
12 module substr_bench
13
14 import opts
15
16 fun bench_rope(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 while cnt != loops do
23 x.substring(0,5)
24 cnt += 1
25 end
26 end
27
28 fun bench_flatstr(nb_cct: Int, loops: Int, strlen: Int)
29 do
30 var a = "a" * strlen
31 var x = a
32 for i in [0 .. nb_cct] do x += a
33 var cnt = 0
34 while cnt != loops do
35 x.substring(0,5)
36 cnt += 1
37 end
38 end
39
40 fun bench_flatbuf(nb_cct: Int, loops: Int, strlen: Int)
41 do
42 var a = "a" * strlen
43 var x = new FlatBuffer.from(a)
44 for i in [0 .. nb_cct] do x.append a
45 var cnt = 0
46 while cnt != loops do
47 x.substring(0,5)
48 cnt += 1
49 end
50 end
51
52 var opts = new OptionContext
53 var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "Mode", -1, "-m")
54 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")
55 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
56 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
57 opts.add_option(mode, nb_ccts, loops, strlen)
58
59 opts.parse(args)
60
61 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
62 opts.usage
63 exit(-1)
64 end
65
66 var modval = mode.value
67
68 if modval == 0 then
69 bench_rope(nb_ccts.value, loops.value, strlen.value)
70 else if modval == 1 then
71 bench_flatstr(nb_ccts.value, loops.value, strlen.value)
72 else if modval == 2 then
73 bench_flatbuf(nb_ccts.value, loops.value, strlen.value)
74 else
75 opts.usage
76 exit(-1)
77 end