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 # Compute the average array length.
25 fun avg_arr_len
: Float do
28 for i
in arr_len
.keys
do
32 return sum
.to_f
/ total
.to_f
35 # Compute the average string length.
36 fun avg_s_len
: Float do
39 for i
in arr_s_len
.keys
do
41 sum
+= arr_s_len
[i
] * i
43 return sum
.to_f
/ total
.to_f
46 # Display statistics in standard output.
48 if arr_len
.sum
== 0 then
49 print
"*** No Array stats ***"
52 print
"*** Array Stats ***"
53 print
"Number of calls to Array::to_s : {sys.arr_len.sum}"
54 print
"Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
55 print
"Average string size in Array : {sys.avg_s_len}"
56 print
"*** End of Stats ***"
74 sys
.arr_len
.inc length
76 sys
.arr_s_len
.inc i
.to_s
.length