benchs/strings: Update string basic functions benchmarks
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 17 Aug 2015 15:02:28 +0000 (11:02 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 21 Aug 2015 14:44:40 +0000 (10:44 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

benchmarks/strings/bench_base.nit [new file with mode: 0644]
benchmarks/strings/chain_concat.nit
benchmarks/strings/index_bench.nit [new file with mode: 0644]
benchmarks/strings/iteration_bench.nit
benchmarks/strings/substr_bench.nit

diff --git a/benchmarks/strings/bench_base.nit b/benchmarks/strings/bench_base.nit
new file mode 100644 (file)
index 0000000..f59c2a8
--- /dev/null
@@ -0,0 +1,26 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT.  This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
+# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You  are  allowed  to  redistribute it and sell it, alone or is a part of
+# another product.
+
+# Base facilities for String-related microbenchmarks
+module bench_base
+
+# Prepares a string to be used in a bench
+fun prepare_string(strlen: Int): String do
+       var s = "a"
+       for i in [1 .. strlen[ do s += "a"
+       return s
+end
+
+# Prepares a buffer to be used in a bench
+fun prepare_buffer(strlen: Int): Buffer do
+       var b = new Buffer
+       for i in [0 .. strlen[ do b.add 'a'
+       return b
+end
index 083d49f..71c8303 100644 (file)
 # Benches measuring the performance of several concatenations on Text variants
 module chain_concat
 
-intrude import standard::text::ropes
+import bench_base
 import opts
 
-redef class FlatString
-       redef fun +(o) do
-               var mlen = length
-               var slen = o.length
-               var nlen = mlen + slen
-               var ns = new NativeString(nlen + 1)
-               items.copy_to(ns, mlen, index_from, 0)
-               if o isa FlatString then
-                       o.items.copy_to(ns, slen, o.index_from, 0)
-               else
-                       var pos = mlen
-                       for i in o.chars do
-                               ns[pos] = i
-                               pos += 1
-                       end
-               end
-               return ns.to_s_with_length(nlen)
-       end
-end
-
-fun bench_flatstr(str_size: Int, nb_ccts: Int, loops: Int)
+private fun bench_string(str_size: Int, nb_ccts: Int, loops: Int)
 do
-       var lft = "a" * str_size
-
+       var lft = prepare_string(str_size)
        for i in [0 .. loops[ do
-               var str: String = ""
+               var str = ""
                for j in [0 .. nb_ccts[ do
                        str += lft
                end
        end
 end
 
-fun bench_ropestr(str_size, nb_ccts, loops: Int) do
-       var lft = "a" * str_size
-
-       for i in [0 .. loops[ do
-               var str: String = ""
-               for j in [0 .. nb_ccts[ do
-                       str = new Concat(str, lft)
-               end
-       end
-end
-
-fun bench_flatbuf(str_size: Int, nb_ccts: Int, loops: Int)
+private fun bench_buffer(str_size: Int, nb_ccts: Int, loops: Int)
 do
-       var lft = "a" * str_size
-
+       var lft = prepare_string(str_size)
        for i in [0 .. loops[ do
-               var buf = new FlatBuffer
-               for j in [0 .. nb_ccts[ do
-                       buf.append(lft)
-               end
-       end
-end
-
-fun bench_ropebuf(str_size: Int, nb_ccts: Int, loops: Int)
-do
-       var lft = "a" * str_size
-
-       for i in [0 .. loops[ do
-               var buf = new RopeBuffer
+               var buf = new Buffer
                for j in [0 .. nb_ccts[ do
                        buf.append(lft)
                end
@@ -82,7 +37,7 @@ do
 end
 
 var opts = new OptionContext
-var mode = new OptionEnum(["flatstr", "ropestr", "flatbuf", "ropebuf"], "Mode", -1, "-m")
+var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
 var nb_ccts = new OptionInt("Number of concatenations per loop", -1, "--ccts")
 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
@@ -98,14 +53,9 @@ end
 var modval = mode.value
 
 if modval == 0 then
-       bench_flatstr(strlen.value, nb_ccts.value, loops.value)
+       bench_string(strlen.value, nb_ccts.value, loops.value)
 else if modval == 1 then
-       bench_ropestr(strlen.value, nb_ccts.value, loops.value)
-else if modval == 2 then
-       bench_flatbuf(strlen.value, nb_ccts.value, loops.value)
-else if modval == 3 then
-       bench_ropebuf(strlen.value, nb_ccts.value, loops.value)
-
+       bench_buffer(strlen.value, nb_ccts.value, loops.value)
 else
        opts.usage
        exit -1
diff --git a/benchmarks/strings/index_bench.nit b/benchmarks/strings/index_bench.nit
new file mode 100644 (file)
index 0000000..3599cee
--- /dev/null
@@ -0,0 +1,62 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# This file is free software, which comes along with NIT.  This software is
+# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A
+# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
+# is kept unaltered, and a notification of the changes is added.
+# You  are  allowed  to  redistribute it and sell it, alone or is a part of
+# another product.
+
+# Benches on the indexed access operation on variants of Text
+module index_bench
+
+import bench_base
+import opts
+
+private fun bench_string(loops: Int, strlen: Int)
+do
+       var a = prepare_string(strlen)
+       var maxl = a.length - 1
+       var cnt = 0
+       while cnt != loops do
+               var c = a[maxl.rand]
+               cnt += 1
+       end
+end
+
+private fun bench_buffer(loops: Int, strlen: Int)
+do
+       var x = prepare_buffer(strlen)
+       var maxl = x.length - 1
+       var cnt = 0
+       while cnt != loops do
+               var c = x[maxl.rand]
+               cnt += 1
+       end
+end
+
+var opts = new OptionContext
+var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
+var loops = new OptionInt("Number of loops to be done", -1, "--loops")
+var strlen = new OptionInt("Length of the base string", -1, "--strlen")
+opts.add_option(mode, loops, strlen)
+
+opts.parse(args)
+
+if loops.value == -1 or strlen.value == -1 then
+       opts.usage
+       exit(-1)
+end
+
+var modval = mode.value
+srand_from(0)
+
+if modval == 0 then
+       bench_string(loops.value, strlen.value)
+else if modval == 1 then
+       bench_buffer(loops.value, strlen.value)
+else
+       opts.usage
+       exit(-1)
+end
index 7f84a34..ea70338 100644 (file)
 module iteration_bench
 
 import opts
-intrude import standard::text::ropes
+import bench_base
 
-redef class Concat
-       redef fun +(o) do
-               var s = o.to_s
-               return new Concat(self, s)
-       end
-end
-
-redef class FlatString
-       redef fun +(o) do
-               var s = o.to_s
-               var b = new FlatBuffer.with_capacity(length + s.length)
-               b.append self
-               for i in s.substrings do b.append i
-               return b.to_s
-       end
-end
-
-fun bench_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_string_iter(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       a = a * nb_cct
+       var a = prepare_string(strlen)
        var cnt = 0
        var c: Char
        while cnt != loops do
@@ -45,10 +27,9 @@ do
        end
 end
 
-fun bench_flatstr_index(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_string_index(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       a = a * nb_cct
+       var a = prepare_string(strlen)
        var cnt = 0
        var c: Char
        var pos = 0
@@ -62,77 +43,9 @@ do
        end
 end
 
-fun bench_ropestr_iter(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       var x: String = new Concat(a, a)
-       for i in [2 .. nb_cct[ do x = new Concat(x, a)
-       var cnt = 0
-       var c: Char
-       while cnt != loops do
-               for i in x do
-                       c = i
-               end
-               cnt += 1
-       end
-end
-
-fun bench_ropestr_index(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       var x: String = new Concat(a, a)
-       for i in [2 .. nb_cct[ do x = new Concat(x, a)
-       var cnt = 0
-       var c: Char
-       var pos = 0
-       while cnt != loops do
-               pos = 0
-               while pos < x.length do
-                       c = x[pos]
-                       pos += 1
-               end
-               cnt += 1
-       end
-end
-
-fun bench_flatbuf_iter(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       a = a * nb_cct
-       var x = new FlatBuffer.from(a)
-       var cnt = 0
-       var c: Char
-       while cnt != loops do
-               for i in x do
-                       c = i
-               end
-               cnt += 1
-       end
-end
-
-fun bench_flatbuf_index(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       a = a * nb_cct
-       var x = new FlatBuffer.from(a)
-       var cnt = 0
-       var c: Char
-       var pos = 0
-       while cnt != loops do
-               pos = 0
-               while pos < x.length do
-                       c = x[pos]
-                       pos += 1
-               end
-               cnt += 1
-       end
-end
-
-fun bench_ropebuf_iter(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_buffer_iter(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       var x = new RopeBuffer.from(a)
-       for i in [0 .. nb_cct[ do x.append a
+       var x = prepare_buffer(strlen)
        var cnt = 0
        var c: Char
        while cnt != loops do
@@ -143,11 +56,9 @@ do
        end
 end
 
-fun bench_ropebuf_index(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_buffer_index(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       var x = new RopeBuffer.from(a)
-       for i in [0 .. nb_cct[ do x.append a
+       var x = prepare_buffer(strlen)
        var cnt = 0
        var c: Char
        var pos = 0
@@ -162,16 +73,15 @@ do
 end
 
 var opts = new OptionContext
-var mode = new OptionEnum(["flatstr", "flatbuf", "ropestr", "ropebuf"], "Mode", -1, "-m")
+var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
 var access_mode = new OptionEnum(["iterator", "index"], "Iteration mode", -1, "--iter-mode")
-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")
 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
-opts.add_option(mode, nb_ccts, loops, strlen, access_mode)
+opts.add_option(mode, loops, strlen, access_mode)
 
 opts.parse(args)
 
-if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
+if loops.value == -1 or strlen.value == -1 then
        opts.usage
        exit(-1)
 end
@@ -181,36 +91,18 @@ var iterval = access_mode.value
 
 if modval == 0 then
        if iterval == 0 then
-               bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value)
+               bench_string_iter(loops.value, strlen.value)
        else if iterval == 1 then
-               bench_flatstr_index(nb_ccts.value, loops.value, strlen.value)
+               bench_string_index(loops.value, strlen.value)
        else
                opts.usage
                exit(-1)
        end
 else if modval == 1 then
        if iterval == 0 then
-               bench_flatbuf_iter(nb_ccts.value, loops.value, strlen.value)
-       else if iterval == 1 then
-               bench_flatbuf_index(nb_ccts.value, loops.value, strlen.value)
-       else
-               opts.usage
-               exit(-1)
-       end
-else if modval == 2 then
-       if iterval == 0 then
-               bench_ropestr_iter(nb_ccts.value, loops.value, strlen.value)
-       else if iterval == 1 then
-               bench_ropestr_index(nb_ccts.value, loops.value, strlen.value)
-       else
-               opts.usage
-               exit(-1)
-       end
-else if modval == 3 then
-       if iterval == 0 then
-               bench_ropebuf_iter(nb_ccts.value, loops.value, strlen.value)
+               bench_buffer_iter(loops.value, strlen.value)
        else if iterval == 1 then
-               bench_ropebuf_index(nb_ccts.value, loops.value, strlen.value)
+               bench_buffer_index(loops.value, strlen.value)
        else
                opts.usage
                exit(-1)
index 4121f7f..c1382ef 100644 (file)
 # Benches on the substring operation on variants of Text
 module substr_bench
 
+import bench_base
 import opts
-intrude import standard::text::ropes
 
-fun bench_flatstr(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_string(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       a = a * nb_cct
+       var a = prepare_string(strlen)
        var maxl = a.length - 1
        var cnt = 0
        while cnt != loops do
@@ -26,38 +25,10 @@ do
        end
 end
 
-fun bench_flatbuf(nb_cct: Int, loops: Int, strlen: Int)
+private fun bench_buffer(loops: Int, strlen: Int)
 do
-       var a = "a" * strlen
-       a = a * nb_cct
-       var maxl = a.length - 1
-       var x = new FlatBuffer.from(a)
-       var cnt = 0
-       while cnt != loops do
-               x.substring(maxl.rand, maxl.rand)
-               cnt += 1
-       end
-end
-
-fun bench_ropestr(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       var x = new Concat(a, a)
-       for i in [2 .. nb_cct[ do x = new Concat(x, a)
-       var maxl = x.length - 1
-       var cnt = 0
-       while cnt != loops do
-               x.substring(maxl.rand, maxl.rand)
-               cnt += 1
-       end
-end
-
-fun bench_ropebuf(nb_cct: Int, loops: Int, strlen: Int)
-do
-       var a = "a" * strlen
-       var x = new RopeBuffer.from(a)
-       for i in [1 .. nb_cct[ do x.append a
-       var maxl = x.length - 1
+       var x = prepare_buffer(strlen)
+       var maxl = x.length
        var cnt = 0
        while cnt != loops do
                x.substring(maxl.rand, maxl.rand)
@@ -66,15 +37,14 @@ do
 end
 
 var opts = new OptionContext
-var mode = new OptionEnum(["flatstr", "flatbuf", "ropestr", "ropebuf"], "Mode", -1, "-m")
-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")
+var mode = new OptionEnum(["string", "buffer"], "Mode", -1, "-m")
 var loops = new OptionInt("Number of loops to be done", -1, "--loops")
 var strlen = new OptionInt("Length of the base string", -1, "--strlen")
-opts.add_option(mode, nb_ccts, loops, strlen)
+opts.add_option(mode, loops, strlen)
 
 opts.parse(args)
 
-if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then
+if loops.value == -1 or strlen.value == -1 then
        opts.usage
        exit(-1)
 end
@@ -83,13 +53,9 @@ var modval = mode.value
 srand_from(0)
 
 if modval == 0 then
-       bench_flatstr(nb_ccts.value, loops.value, strlen.value)
+       bench_string(loops.value, strlen.value)
 else if modval == 1 then
-       bench_flatbuf(nb_ccts.value, loops.value, strlen.value)
-else if modval == 2 then
-       bench_ropestr(nb_ccts.value, loops.value, strlen.value)
-else if modval == 3 then
-       bench_ropebuf(nb_ccts.value, loops.value, strlen.value)
+       bench_buffer(loops.value, strlen.value)
 else
        opts.usage
        exit(-1)