Merge: lib/std/union_find: make DisjointSet Cloneable
[nit.git] / benchmarks / strings / utf_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 utf_iteration_bench
13
14 import opts
15 import string_experimentations::utf8_noindex
16
17 fun bench_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int)
18 do
19 var a = "a" * strlen
20 var x = a.as(FlatString)
21 for i in [0 .. nb_cct] do x = (x + a).as(FlatString)
22 var cnt = 0
23 var c: UnicodeChar
24 while cnt != loops do
25 var it = new FlatStringIter(x)
26 for i in it do
27 c = i
28 end
29 cnt += 1
30 end
31 end
32
33 fun bench_flatstr_index(nb_cct: Int, loops: Int, strlen: Int)
34 do
35 var a = "a" * strlen
36 var x = a.as(FlatString)
37 for i in [0 .. nb_cct] do x = (x + a).as(FlatString)
38 var cnt = 0
39 var c: UnicodeChar
40 var pos = 0
41 while cnt != loops do
42 pos = 0
43 while pos < x.length do
44 c = x.char_at(pos)
45 pos += 1
46 end
47 cnt += 1
48 end
49 end
50
51 var opts = new OptionContext
52 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
53 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")
54 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
55 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
56 opts.add_option(nb_ccts, loops, strlen, access_mode)
57
58 opts.parse(args)
59
60 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
61 opts.usage
62 exit(-1)
63 end
64
65 var iterval = access_mode.value
66
67 if iterval == 0 then
68 bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value)
69 else if iterval == 1 then
70 bench_flatstr_index(nb_ccts.value, loops.value, strlen.value)
71 else
72 opts.usage
73 exit(-1)
74 end