benchmarks/string: Updated bench scripts for strings
[nit.git] / benchmarks / strings / chain_concat.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 measuring the performance of several concatenations on Text variants
12 module chain_concat
13
14 intrude import standard::ropes
15 import opts
16
17 redef class FlatString
18 redef fun +(o) do
19 var mlen = length
20 var slen = o.length
21 var nlen = mlen + slen
22 var ns = new NativeString(nlen + 1)
23 items.copy_to(ns, mlen, index_from, 0)
24 if o isa FlatString then
25 o.items.copy_to(ns, slen, o.index_from, 0)
26 else
27 var pos = mlen
28 for i in o.chars do
29 ns[pos] = i
30 pos += 1
31 end
32 end
33 return ns.to_s_with_length(nlen)
34 end
35 end
36
37 fun bench_flatstr(str_size: Int, nb_ccts: Int, loops: Int)
38 do
39 var lft = "a" * str_size
40
41 for i in [0 .. loops[ do
42 var str: String = ""
43 for j in [0 .. nb_ccts[ do
44 str += lft
45 end
46 end
47 end
48
49 fun bench_ropestr(str_size, nb_ccts, loops: Int) do
50 var lft = "a" * str_size
51
52 for i in [0 .. loops[ do
53 var str: String = ""
54 for j in [0 .. nb_ccts[ do
55 str = new Concat(str, lft)
56 end
57 end
58 end
59
60 fun bench_flatbuf(str_size: Int, nb_ccts: Int, loops: Int)
61 do
62 var lft = "a" * str_size
63
64 for i in [0 .. loops[ do
65 var buf = new FlatBuffer
66 for j in [0 .. nb_ccts[ do
67 buf.append(lft)
68 end
69 end
70 end
71
72 fun bench_ropebuf(str_size: Int, nb_ccts: Int, loops: Int)
73 do
74 var lft = "a" * str_size
75
76 for i in [0 .. loops[ do
77 var buf = new RopeBuffer
78 for j in [0 .. nb_ccts[ do
79 buf.append(lft)
80 end
81 end
82 end
83
84 var opts = new OptionContext
85 var mode = new OptionEnum(["flatstr", "ropestr", "flatbuf", "ropebuf"], "Mode", -1, "-m")
86 var nb_ccts = new OptionInt("Number of concatenations per loop", -1, "--ccts")
87 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
88 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
89 opts.add_option(mode, nb_ccts, loops, strlen)
90
91 opts.parse(args)
92
93 if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
94 opts.usage
95 exit -1
96 end
97
98 var modval = mode.value
99
100 if modval == 0 then
101 bench_flatstr(strlen.value, nb_ccts.value, loops.value)
102 else if modval == 1 then
103 bench_ropestr(strlen.value, nb_ccts.value, loops.value)
104 else if modval == 2 then
105 bench_flatbuf(strlen.value, nb_ccts.value, loops.value)
106 else if modval == 3 then
107 bench_ropebuf(strlen.value, nb_ccts.value, loops.value)
108
109 else
110 opts.usage
111 exit -1
112 end