From: Lucas Bajolet Date: Mon, 17 Aug 2015 15:02:28 +0000 (-0400) Subject: benchs/strings: Update string basic functions benchmarks X-Git-Tag: v0.7.8~73^2~4 X-Git-Url: http://nitlanguage.org benchs/strings: Update string basic functions benchmarks Signed-off-by: Lucas Bajolet --- diff --git a/benchmarks/strings/bench_base.nit b/benchmarks/strings/bench_base.nit new file mode 100644 index 0000000..f59c2a8 --- /dev/null +++ b/benchmarks/strings/bench_base.nit @@ -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 diff --git a/benchmarks/strings/chain_concat.nit b/benchmarks/strings/chain_concat.nit index 083d49f..71c8303 100644 --- a/benchmarks/strings/chain_concat.nit +++ b/benchmarks/strings/chain_concat.nit @@ -11,70 +11,25 @@ # 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 index 0000000..3599cee --- /dev/null +++ b/benchmarks/strings/index_bench.nit @@ -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 diff --git a/benchmarks/strings/iteration_bench.nit b/benchmarks/strings/iteration_bench.nit index 7f84a34..ea70338 100644 --- a/benchmarks/strings/iteration_bench.nit +++ b/benchmarks/strings/iteration_bench.nit @@ -12,29 +12,11 @@ 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) diff --git a/benchmarks/strings/substr_bench.nit b/benchmarks/strings/substr_bench.nit index 4121f7f..c1382ef 100644 --- a/benchmarks/strings/substr_bench.nit +++ b/benchmarks/strings/substr_bench.nit @@ -11,13 +11,12 @@ # 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)