benchmarks/string: Updated bench scripts for strings
[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 intrude import standard::ropes
16
17 fun bench_flatstr(nb_cct: Int, loops: Int, strlen: Int)
18 do
19 var a = "a" * strlen
20 a = a * nb_cct
21 var maxl = a.length - 1
22 var cnt = 0
23 while cnt != loops do
24 a.substring(maxl.rand, maxl.rand)
25 cnt += 1
26 end
27 end
28
29 fun bench_flatbuf(nb_cct: Int, loops: Int, strlen: Int)
30 do
31 var a = "a" * strlen
32 a = a * nb_cct
33 var maxl = a.length - 1
34 var x = new FlatBuffer.from(a)
35 var cnt = 0
36 while cnt != loops do
37 x.substring(maxl.rand, maxl.rand)
38 cnt += 1
39 end
40 end
41
42 fun bench_ropestr(nb_cct: Int, loops: Int, strlen: Int)
43 do
44 var a = "a" * strlen
45 var x = new Concat(a, a)
46 for i in [2 .. nb_cct[ do x = new Concat(x, a)
47 var maxl = x.length - 1
48 var cnt = 0
49 while cnt != loops do
50 x.substring(maxl.rand, maxl.rand)
51 cnt += 1
52 end
53 end
54
55 fun bench_ropebuf(nb_cct: Int, loops: Int, strlen: Int)
56 do
57 var a = "a" * strlen
58 var x = new RopeBuffer.from(a)
59 for i in [1 .. nb_cct[ do x.append a
60 var maxl = x.length - 1
61 var cnt = 0
62 while cnt != loops do
63 x.substring(maxl.rand, maxl.rand)
64 cnt += 1
65 end
66 end
67
68 var opts = new OptionContext
69 var mode = new OptionEnum(["flatstr", "flatbuf", "ropestr", "ropebuf"], "Mode", -1, "-m")
70 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")
71 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
72 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
73 opts.add_option(mode, nb_ccts, loops, strlen)
74
75 opts.parse(args)
76
77 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
78 opts.usage
79 exit(-1)
80 end
81
82 var modval = mode.value
83 srand_from(0)
84
85 if modval == 0 then
86 bench_flatstr(nb_ccts.value, loops.value, strlen.value)
87 else if modval == 1 then
88 bench_flatbuf(nb_ccts.value, loops.value, strlen.value)
89 else if modval == 2 then
90 bench_ropestr(nb_ccts.value, loops.value, strlen.value)
91 else if modval == 3 then
92 bench_ropebuf(nb_ccts.value, loops.value, strlen.value)
93 else
94 opts.usage
95 exit(-1)
96 end