benchs/strings: Update string basic functions benchmarks
[nit.git] / benchmarks / strings / index_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 indexed access operation on variants of Text
12 module index_bench
13
14 import bench_base
15 import opts
16
17 private fun bench_string(loops: Int, strlen: Int)
18 do
19 var a = prepare_string(strlen)
20 var maxl = a.length - 1
21 var cnt = 0
22 while cnt != loops do
23 var c = a[maxl.rand]
24 cnt += 1
25 end
26 end
27
28 private fun bench_buffer(loops: Int, strlen: Int)
29 do
30 var x = prepare_buffer(strlen)
31 var maxl = x.length - 1
32 var cnt = 0
33 while cnt != loops do
34 var c = x[maxl.rand]
35 cnt += 1
36 end
37 end
38
39 var opts = new OptionContext
40 var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
41 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
42 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
43 opts.add_option(mode, loops, strlen)
44
45 opts.parse(args)
46
47 if loops.value == -1 or strlen.value == -1 then
48 opts.usage
49 exit(-1)
50 end
51
52 var modval = mode.value
53 srand_from(0)
54
55 if modval == 0 then
56 bench_string(loops.value, strlen.value)
57 else if modval == 1 then
58 bench_buffer(loops.value, strlen.value)
59 else
60 opts.usage
61 exit(-1)
62 end