lib/array_debug: Added array_debug module to keep stats on Array use in a program.
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 26 Aug 2014 14:40:12 +0000 (10:40 -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>

lib/array_debug.nit [new file with mode: 0644]
tests/sav/array_debug.res [new file with mode: 0644]

diff --git a/lib/array_debug.nit b/lib/array_debug.nit
new file mode 100644 (file)
index 0000000..f112d7a
--- /dev/null
@@ -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.
+
+# Exposes functions to help profile or debug Arrays.
+module array_debug
+
+import counter
+
+redef class Sys
+
+       # Tracks the average length of the Strings of an array when calling to_s
+       var arr_s_len = new Counter[Int]
+
+       # Keeps the average length of an Array when calling to_s
+       var arr_len = new Counter[Int]
+
+       fun avg_arr_len: Float do
+               var total = 0
+               var sum = 0
+               for i in arr_len.keys do
+                       total += arr_len[i]
+                       sum += arr_len[i] * i
+               end
+               return sum.to_f / total.to_f
+       end
+
+       fun avg_s_len: Float do
+               var total = 0
+               var sum = 0
+               for i in arr_s_len.keys do
+                       total += arr_s_len[i]
+                       sum += arr_s_len[i] * i
+               end
+               return sum.to_f / total.to_f
+       end
+
+       fun print_stats do
+               if arr_len.sum == 0 then
+                       print "*** No Array stats ***"
+                       return
+               end
+               print "*** Array Stats ***"
+               print "Number of calls to Array::to_s : {sys.arr_len.sum}"
+               print "Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
+               print "Average string size in Array : {sys.avg_s_len}"
+               print "*** End of Stats ***"
+       end
+
+       redef fun run do
+               super
+               print_stats
+       end
+end
+
+redef fun exit(i)
+do
+       sys.print_stats
+       super
+end
+
+redef class Array[E]
+
+       redef fun to_s do
+               sys.arr_len.inc length
+               for i in self do
+                       sys.arr_s_len.inc i.to_s.length
+               end
+               return super
+       end
+
+end
diff --git a/tests/sav/array_debug.res b/tests/sav/array_debug.res
new file mode 100644 (file)
index 0000000..c4b2490
--- /dev/null
@@ -0,0 +1 @@
+*** No Array stats ***