benchs/strings: Update string basic functions benchmarks
[nit.git] / benchmarks / strings / iteration_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 for iteration on variants of Text
12 module iteration_bench
13
14 import opts
15 import bench_base
16
17 private fun bench_string_iter(loops: Int, strlen: Int)
18 do
19 var a = prepare_string(strlen)
20 var cnt = 0
21 var c: Char
22 while cnt != loops do
23 for i in a do
24 c = i
25 end
26 cnt += 1
27 end
28 end
29
30 private fun bench_string_index(loops: Int, strlen: Int)
31 do
32 var a = prepare_string(strlen)
33 var cnt = 0
34 var c: Char
35 var pos = 0
36 while cnt != loops do
37 pos = 0
38 while pos < a.length do
39 c = a[pos]
40 pos += 1
41 end
42 cnt += 1
43 end
44 end
45
46 private fun bench_buffer_iter(loops: Int, strlen: Int)
47 do
48 var x = prepare_buffer(strlen)
49 var cnt = 0
50 var c: Char
51 while cnt != loops do
52 for i in x do
53 c = i
54 end
55 cnt += 1
56 end
57 end
58
59 private fun bench_buffer_index(loops: Int, strlen: Int)
60 do
61 var x = prepare_buffer(strlen)
62 var cnt = 0
63 var c: Char
64 var pos = 0
65 while cnt != loops do
66 pos = 0
67 while pos < x.length do
68 c = x[pos]
69 pos += 1
70 end
71 cnt += 1
72 end
73 end
74
75 var opts = new OptionContext
76 var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
77 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
78 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
79 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
80 opts.add_option(mode, loops, strlen, access_mode)
81
82 opts.parse(args)
83
84 if loops.value == -1 or strlen.value == -1 then
85 opts.usage
86 exit(-1)
87 end
88
89 var modval = mode.value
90 var iterval = access_mode.value
91
92 if modval == 0 then
93 if iterval == 0 then
94 bench_string_iter(loops.value, strlen.value)
95 else if iterval == 1 then
96 bench_string_index(loops.value, strlen.value)
97 else
98 opts.usage
99 exit(-1)
100 end
101 else if modval == 1 then
102 if iterval == 0 then
103 bench_buffer_iter(loops.value, strlen.value)
104 else if iterval == 1 then
105 bench_buffer_index(loops.value, strlen.value)
106 else
107 opts.usage
108 exit(-1)
109 end
110 else
111 opts.usage
112 exit(-1)
113 end