From a4e8f915663b4f07813b8ffeb8a998f1cafdbed0 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 21 Aug 2014 15:23:59 -0400 Subject: [PATCH] benchmarks/string: Added benchmark for Array.to_s Signed-off-by: Lucas Bajolet --- benchmarks/bench_strings.sh | 78 ++++++++++++++++++++ .../strings/array_to_s_vars/array_to_s_buffer.nit | 29 ++++++++ .../strings/array_to_s_vars/array_to_s_flatstr.nit | 31 ++++++++ .../strings/array_to_s_vars/array_to_s_man_buf.nit | 41 ++++++++++ .../strings/array_to_s_vars/array_to_s_manual.nit | 59 +++++++++++++++ .../strings/array_to_s_vars/array_to_s_rope.nit | 31 ++++++++ benchmarks/strings/array_tos.nit | 41 ++++++++++ 7 files changed, 310 insertions(+) create mode 100644 benchmarks/strings/array_to_s_vars/array_to_s_buffer.nit create mode 100644 benchmarks/strings/array_to_s_vars/array_to_s_flatstr.nit create mode 100644 benchmarks/strings/array_to_s_vars/array_to_s_man_buf.nit create mode 100644 benchmarks/strings/array_to_s_vars/array_to_s_manual.nit create mode 100644 benchmarks/strings/array_to_s_vars/array_to_s_rope.nit create mode 100644 benchmarks/strings/array_tos.nit diff --git a/benchmarks/bench_strings.sh b/benchmarks/bench_strings.sh index 54191a4..c327d58 100755 --- a/benchmarks/bench_strings.sh +++ b/benchmarks/bench_strings.sh @@ -36,6 +36,8 @@ function usage() echo " - usage : cct max_nb_cct loops strlen" echo " substr: substring benching" echo " - usage : substr max_nb_cct loops strlen" + echo " array: Benchmark for the to_s in array" + echo " - usage : array nb_cct loops max_arrlen" } function benches() @@ -43,6 +45,81 @@ function benches() bench_concat $@; bench_iteration $@; bench_substr $@; + bench_array $@; +} + +function bench_array() +{ + if $verbose; then + echo "*** Benching Array.to_s performance ***" + fi + + ../bin/nitg --global ./strings/array_tos.nit -m ./strings/array_to_s_vars/array_to_s_rope.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\"" + + prepare_res arr_tos_ropes.out arr_tos_ropes ropes + if $verbose; then + echo "Ropes :" + fi + for i in `seq 1 "$3"`; do + if $verbose; then + echo "String length = $i, Concats/loop = $1, Loops = $2" + fi + bench_command $i ropes$i ./array_tos --loops $2 --strlen $i --ccts $1 "NIT_GC_CHOOSER=large" + done + + ../bin/nitg --global ./strings/array_tos.nit -m ./strings/array_to_s_vars/array_to_s_flatstr.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\"" + + prepare_res arr_tos_flat.out arr_tos_flat flatstring + if $verbose; then + echo "FlatStrings :" + fi + for i in `seq 1 "$3"`; do + if $verbose; then + echo "String length = $i, Concats/loop = $1, Loops = $2" + fi + bench_command $i flatstring$i ./array_tos --loops $2 --strlen $i --ccts $1 "NIT_GC_CHOOSER=large" + done + + ../bin/nitg --global ./strings/array_tos.nit -m ./strings/array_to_s_vars/array_to_s_buffer.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\"" + + prepare_res arr_tos_buf.out arr_tos_buf flatbuffer + if $verbose; then + echo "FlatBuffers :" + fi + for i in `seq 1 "$3"`; do + if $verbose; then + echo "String length = $i, Concats/loop = $1, Loops = $2" + fi + bench_command $i flatbuffer$i ./array_tos --loops $2 --strlen $i --ccts $1 "NIT_GC_CHOOSER=large" + done + + ../bin/nitg --global ./strings/array_tos.nit -m ./strings/array_to_s_vars/array_to_s_manual.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\"" + + prepare_res arr_tos_man.out arr_tos_man memmove + if $verbose; then + echo "Memmove :" + fi + for i in `seq 1 "$3"`; do + if $verbose; then + echo "String length = $i, Concats/loop = $1, Loops = $2" + fi + bench_command $i memmove$i ./array_tos --loops $2 --strlen $i --ccts $1 "NIT_GC_CHOOSER=large" + done + + ../bin/nitg --global ./strings/array_tos.nit -m ./strings/array_to_s_vars/array_to_s_man_buf.nit --make-flags "CFLAGS=\"-g -O2 -DNOBOEHM\"" + + prepare_res arr_tos_man_buf.out arr_tos_man_buf flatbuf_with_capacity + if $verbose; then + echo "Memmove :" + fi + for i in `seq 1 "$3"`; do + if $verbose; then + echo "String length = $i, Concats/loop = $1, Loops = $2" + fi + bench_command $i flatbuf_with_capacity$i ./array_tos --loops $2 --strlen $i --ccts $1 "NIT_GC_CHOOSER=large" + done + + plot array_tos.gnu } function bench_concat() @@ -278,6 +355,7 @@ case "$1" in iter) shift; bench_iteration $@ ;; cct) shift; bench_concat $@ ;; substr) shift; bench_substr $@ ;; + array) shift; bench_array $@ ;; all) shift; benches $@ ;; *) usage; exit;; esac diff --git a/benchmarks/strings/array_to_s_vars/array_to_s_buffer.nit b/benchmarks/strings/array_to_s_vars/array_to_s_buffer.nit new file mode 100644 index 0000000..d61b742 --- /dev/null +++ b/benchmarks/strings/array_to_s_vars/array_to_s_buffer.nit @@ -0,0 +1,29 @@ +# 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. + +# Implementation of Array::to_s with FlatBuffer exclusively +# +# To be used as a Mixin at compile-time for benchmarking purposes. +module array_to_s_buffer + +redef class Array[E] + redef fun to_s: String do + var s = new FlatBuffer + var i = 0 + var l = length + var its = _items + while i < l do + var e = its[i] + if e != null then s.append(e.to_s) + i += 1 + end + return s.to_s + end +end diff --git a/benchmarks/strings/array_to_s_vars/array_to_s_flatstr.nit b/benchmarks/strings/array_to_s_vars/array_to_s_flatstr.nit new file mode 100644 index 0000000..0240d5b --- /dev/null +++ b/benchmarks/strings/array_to_s_vars/array_to_s_flatstr.nit @@ -0,0 +1,31 @@ +# 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. + +# Implementation of Array::to_s with FlatStrings exclusively +# +# To be used as a Mixin at compile-time for benchmarking purposes. +module array_to_s_flatstr + +redef class Array[E] + + redef fun to_s do + var i = 1 + var l = length + if l == 0 then return "" + var its = _items + var s = its[0].to_s + while i < l do + var e = its[i] + if e != null then s += e.to_s + i += 1 + end + return s + end +end diff --git a/benchmarks/strings/array_to_s_vars/array_to_s_man_buf.nit b/benchmarks/strings/array_to_s_vars/array_to_s_man_buf.nit new file mode 100644 index 0000000..ec81322 --- /dev/null +++ b/benchmarks/strings/array_to_s_vars/array_to_s_man_buf.nit @@ -0,0 +1,41 @@ +# 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. + +# Implementation of Array::to_s with manual FlatBuffer (precalc its capacity) +# +# To be used as a Mixin at compile-time for benchmarking purposes. +module array_to_s_man_buf + +redef class NativeArray[E] + new(length: Int) is intern +end + +redef class Array[E] + redef fun to_s: String do + var l = length + var its = _items + var na = new NativeArray[String](l) + var i = 0 + var sl = 0 + while i < l do + var tmp = its[i].to_s + sl += tmp.length + na[i] = tmp + i += 1 + end + var ns = new FlatBuffer.with_capacity(sl) + i = 0 + while i < l do + ns.append na[i] + i += 1 + end + return ns.to_s + end +end diff --git a/benchmarks/strings/array_to_s_vars/array_to_s_manual.nit b/benchmarks/strings/array_to_s_vars/array_to_s_manual.nit new file mode 100644 index 0000000..843eb93 --- /dev/null +++ b/benchmarks/strings/array_to_s_vars/array_to_s_manual.nit @@ -0,0 +1,59 @@ +# 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. + +# Implementation of Array::to_s with manual management of the String +# +# To be used as a Mixin at compile-time for benchmarking purposes. +module array_to_s_manual + +intrude import standard::string +intrude import standard::collection::array + +redef class NativeArray[E] + new(length: Int) is intern +end + +redef class Array[E] + super StringCapable + + redef fun to_s: String do + var l = length + var its = _items + var na = new NativeArray[String](l) + var i = 0 + var sl = 0 + while i < l do + var tmp = its[i].to_s + sl += tmp.length + na[i] = tmp + i += 1 + end + var ns = calloc_string(sl + 1) + ns[sl] = '\0' + i = 0 + var off = 0 + while i < l do + var tmp = na[i] + var tpl = tmp.length + if tmp isa FlatString then + tmp.items.copy_to(ns, tpl, tmp.index_from, off) + off += tpl + else + for j in tmp.substrings do + var s = j.as(FlatString) + s.items.copy_to(ns, tpl, s.index_from, off) + off += tpl + end + end + i += 1 + end + return ns.to_s_with_length(sl) + end +end diff --git a/benchmarks/strings/array_to_s_vars/array_to_s_rope.nit b/benchmarks/strings/array_to_s_vars/array_to_s_rope.nit new file mode 100644 index 0000000..c162dff --- /dev/null +++ b/benchmarks/strings/array_to_s_vars/array_to_s_rope.nit @@ -0,0 +1,31 @@ +# 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. + +# Implementation of Array::to_s with a Rope exclusively +# +# To be used as a Mixin at compile-time for benchmarking purposes. +module array_to_s_rope + +redef class Array[E] + + redef fun to_s do + var i = 1 + var l = length + if l == 0 then return "" + var s: String = new RopeString.from(self[0].to_s) + var its = _items + while i < l do + var e = its[i] + if e != null then s += e.to_s + i += 1 + end + return s + end +end diff --git a/benchmarks/strings/array_tos.nit b/benchmarks/strings/array_tos.nit new file mode 100644 index 0000000..8dff9e4 --- /dev/null +++ b/benchmarks/strings/array_tos.nit @@ -0,0 +1,41 @@ +# 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. + +# Benchmark on the Array.to_s function using several methods. +module array_tos + +import opts + +fun bench_array(arr_size, item_size, loops: Int) +do + var arr = new Array[String].with_capacity(arr_size) + var s = "a" * item_size + + for i in [0 .. arr_size[ do arr.push s + + for i in [0..loops[ do + s = arr.to_s + end +end + +var opts = new OptionContext +var nb_ccts = new OptionInt("Size of an element", -1, "--ccts") +var loops = new OptionInt("Number of loops to be done", -1, "--loops") +var strlen = new OptionInt("Length of the Array", -1, "--strlen") +opts.add_option(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 + +bench_array(strlen.value, nb_ccts.value, loops.value) -- 1.7.9.5