From: Lucas Bajolet Date: Tue, 29 Jul 2014 17:20:36 +0000 (-0400) Subject: benchmarks/string: Added String benchmarks X-Git-Tag: v0.6.7~11^2~1 X-Git-Url: http://nitlanguage.org benchmarks/string: Added String benchmarks Signed-off-by: Lucas Bajolet --- diff --git a/benchmarks/strings/chain_concat.nit b/benchmarks/strings/chain_concat.nit new file mode 100644 index 0000000..35466f9 --- /dev/null +++ b/benchmarks/strings/chain_concat.nit @@ -0,0 +1,78 @@ +# 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 measuring the performance of several concatenations on Text variants +module chain_concat + +import opts + +fun bench_flatstr(str_size: Int, nb_ccts: Int, loops: Int) +do + var lft = "a" * str_size + + for i in [0..loops] do + var str: String = lft + for j in [0..nb_ccts] do + str += lft + end + end +end + +fun bench_rope(str_size: Int, nb_ccts: Int, loops: Int) +do + var lft = "a" * str_size + + for i in [0..loops] do + var str: String = new RopeString.from(lft) + for j in [0..nb_ccts] do + str += lft + end + end +end + +fun bench_flatbuf(str_size: Int, nb_ccts: Int, loops: Int) +do + var lft = "a" * str_size + + for i in [0..loops] do + var buf = new FlatBuffer.from(lft) + for j in [0..nb_ccts] do + buf.append(lft) + end + buf.to_s + end +end + +var opts = new OptionContext +var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "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") +opts.add_option(mode, nb_ccts, loops, strlen) + +opts.parse(args) + +if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then + opts.usage + exit -1 +end + +var modval = mode.value + +if modval == 0 then + bench_rope(strlen.value, nb_ccts.value, loops.value) +else if modval == 1 then + bench_flatstr(strlen.value, nb_ccts.value, loops.value) +else if modval == 2 then + bench_flatbuf(strlen.value, nb_ccts.value, loops.value) +else + opts.usage + exit -1 +end diff --git a/benchmarks/strings/iteration_bench.nit b/benchmarks/strings/iteration_bench.nit new file mode 100644 index 0000000..a8047d2 --- /dev/null +++ b/benchmarks/strings/iteration_bench.nit @@ -0,0 +1,163 @@ +# 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 for iteration on variants of Text +module iteration_bench + +import opts + +fun bench_rope_iter(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x:String = new RopeString.from(a) + for i in [0 .. nb_cct] do 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_rope_index(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x:String = new RopeString.from(a) + for i in [0 .. nb_cct] do 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_flatstr_iter(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x = a + for i in [0 .. nb_cct] do 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_flatstr_index(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x = a + for i in [0 .. nb_cct] do 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 + var x = new FlatBuffer.from(a) + for i in [0 .. nb_cct] do x.append 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 + var x = new FlatBuffer.from(a) + for i in [0 .. nb_cct] do x.append 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 + +var opts = new OptionContext +var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "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.parse(args) + +if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then + opts.usage + exit(-1) +end + +var modval = mode.value +var iterval = access_mode.value + +if modval == 0 then + if iterval == 0 then + bench_rope_iter(nb_ccts.value, loops.value, strlen.value) + else if iterval == 1 then + bench_rope_index(nb_ccts.value, loops.value, strlen.value) + else + opts.usage + exit(-1) + end +else if modval == 1 then + if iterval == 0 then + bench_flatstr_iter(nb_ccts.value, loops.value, strlen.value) + else if iterval == 1 then + bench_flatstr_index(nb_ccts.value, loops.value, strlen.value) + else + opts.usage + exit(-1) + end +else if modval == 2 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 + opts.usage + exit(-1) +end diff --git a/benchmarks/strings/substr_bench.nit b/benchmarks/strings/substr_bench.nit new file mode 100644 index 0000000..5021e66 --- /dev/null +++ b/benchmarks/strings/substr_bench.nit @@ -0,0 +1,77 @@ +# 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 substring operation on variants of Text +module substr_bench + +import opts + +fun bench_rope(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x:String = new RopeString.from(a) + for i in [0 .. nb_cct] do x += a + var cnt = 0 + while cnt != loops do + x.substring(0,5) + cnt += 1 + end +end + +fun bench_flatstr(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x = a + for i in [0 .. nb_cct] do x += a + var cnt = 0 + while cnt != loops do + x.substring(0,5) + cnt += 1 + end +end + +fun bench_flatbuf(nb_cct: Int, loops: Int, strlen: Int) +do + var a = "a" * strlen + var x = new FlatBuffer.from(a) + for i in [0 .. nb_cct] do x.append a + var cnt = 0 + while cnt != loops do + x.substring(0,5) + cnt += 1 + end +end + +var opts = new OptionContext +var mode = new OptionEnum(["rope", "flatstr", "flatbuf"], "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 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.parse(args) + +if nb_ccts.value == -1 or loops.value == -1 or strlen.value == -1 then + opts.usage + exit(-1) +end + +var modval = mode.value + +if modval == 0 then + bench_rope(nb_ccts.value, loops.value, strlen.value) +else if modval == 1 then + bench_flatstr(nb_ccts.value, loops.value, strlen.value) +else if modval == 2 then + bench_flatbuf(nb_ccts.value, loops.value, strlen.value) +else + opts.usage + exit(-1) +end