1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
11 # Benches measuring the performance of several concatenations on Text variants
14 intrude import standard
::ropes
17 redef class FlatString
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)
33 return ns
.to_s_with_length
(nlen
)
37 fun bench_flatstr
(str_size
: Int, nb_ccts
: Int, loops
: Int)
39 var lft
= "a" * str_size
41 for i
in [0 .. loops
[ do
43 for j
in [0 .. nb_ccts
[ do
49 fun bench_ropestr
(str_size
, nb_ccts
, loops
: Int) do
50 var lft
= "a" * str_size
52 for i
in [0 .. loops
[ do
54 for j
in [0 .. nb_ccts
[ do
55 str
= new Concat(str
, lft
)
60 fun bench_flatbuf
(str_size
: Int, nb_ccts
: Int, loops
: Int)
62 var lft
= "a" * str_size
64 for i
in [0 .. loops
[ do
65 var buf
= new FlatBuffer
66 for j
in [0 .. nb_ccts
[ do
72 fun bench_ropebuf
(str_size
: Int, nb_ccts
: Int, loops
: Int)
74 var lft
= "a" * str_size
76 for i
in [0 .. loops
[ do
77 var buf
= new RopeBuffer
78 for j
in [0 .. nb_ccts
[ do
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
)
93 if nb_ccts
.value
== -1 or loops
.value
== -1 or strlen
.value
== -1 then
98 var modval
= mode
.value
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
)