1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Exposes functions to help profile or debug Arrays.
18 # Tracks the average length of the Strings of an array when calling to_s
19 var arr_s_len
= new Counter[Int]
21 # Keeps the average length of an Array when calling to_s
22 var arr_len
= new Counter[Int]
24 fun avg_arr_len
: Float do
27 for i
in arr_len
.keys
do
31 return sum
.to_f
/ total
.to_f
34 fun avg_s_len
: Float do
37 for i
in arr_s_len
.keys
do
39 sum
+= arr_s_len
[i
] * i
41 return sum
.to_f
/ total
.to_f
45 if arr_len
.sum
== 0 then
46 print
"*** No Array stats ***"
49 print
"*** Array Stats ***"
50 print
"Number of calls to Array::to_s : {sys.arr_len.sum}"
51 print
"Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
52 print
"Average string size in Array : {sys.avg_s_len}"
53 print
"*** End of Stats ***"
71 sys
.arr_len
.inc length
73 sys
.arr_s_len
.inc i
.to_s
.length