effb9ec7354f6e44853917413a69784e8b614fdb
[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_flatstr(nb_cct: Int, loops: Int, strlen: Int)
17 do
18 var a = "a" * strlen
19 var x = 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_flatbuf(nb_cct: Int, loops: Int, strlen: Int)
29 do
30 var a = "a" * strlen
31 var x = new FlatBuffer.from(a)
32 for i in [0 .. nb_cct] do x.append a
33 var cnt = 0
34 while cnt != loops do
35 x.substring(0,5)
36 cnt += 1
37 end
38 end
39
40 var opts = new OptionContext
41 var mode = new OptionEnum(["flatstr", "flatbuf"], "Mode", -1, "-m")
42 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")
43 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
44 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
45 opts.add_option(mode, nb_ccts, loops, strlen)
46
47 opts.parse(args)
48
49 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
50 opts.usage
51 exit(-1)
52 end
53
54 var modval = mode.value
55
56 if modval == 0 then
57 bench_flatstr(nb_ccts.value, loops.value, strlen.value)
58 else if modval == 1 then
59 bench_flatbuf(nb_ccts.value, loops.value, strlen.value)
60 else
61 opts.usage
62 exit(-1)
63 end