benchmarks/string: Added benchmark for Array.to_s
authorLucas Bajolet <r4pass@hotmail.com>
Thu, 21 Aug 2014 19:23:59 +0000 (15:23 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 26 Aug 2014 15:30:56 +0000 (11:30 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

benchmarks/bench_strings.sh
benchmarks/strings/array_to_s_vars/array_to_s_buffer.nit [new file with mode: 0644]
benchmarks/strings/array_to_s_vars/array_to_s_flatstr.nit [new file with mode: 0644]
benchmarks/strings/array_to_s_vars/array_to_s_man_buf.nit [new file with mode: 0644]
benchmarks/strings/array_to_s_vars/array_to_s_manual.nit [new file with mode: 0644]
benchmarks/strings/array_to_s_vars/array_to_s_rope.nit [new file with mode: 0644]
benchmarks/strings/array_tos.nit [new file with mode: 0644]

index 54191a4..c327d58 100755 (executable)
@@ -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 (file)
index 0000000..d61b742
--- /dev/null
@@ -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 (file)
index 0000000..0240d5b
--- /dev/null
@@ -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 (file)
index 0000000..ec81322
--- /dev/null
@@ -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 (file)
index 0000000..843eb93
--- /dev/null
@@ -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 (file)
index 0000000..c162dff
--- /dev/null
@@ -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 (file)
index 0000000..8dff9e4
--- /dev/null
@@ -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)