summaryrefslogtreecommitdiff
path: root/ipl/procs/ibench.icn
blob: ae473702febb8387126754b6cf816cb1883a1e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
############################################################################
#
#	File:     ibench.icn
#
#	Subject:  Procedures to support Icon benchmarking
#
#	Author:   Ralph E. Griswold
#
#	Date:     March 23, 2002
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#     Procedures to support benchmarking of Icon programs:
#
#	 Init__(prog)		initialize for benchmarking
#	 Term__()		terminate benchmarking
#	 Allocated__()		get amounts allocated
#	 Collections__()	get collections
#	 Regions__()		get regions
#	 Signature__()		show program/environment information
#	 Storage__()		get storage
#	 Time__()		show elapsed time
#	 Display__(data,name)	show information
#
############################################################################
#
#     The code to be timed is bracketed by calls to Init__(name)
#  and Term__(), where name is used for tagging the results.
#  The typical usage is:
#
#	procedure main()
#	   [declarations]
#	   Init__(name)
#		.
#		.
#		.
#	   Term__()
#	end
#
#     If the environment variable OUTPUT is set, program output is
#  not suppressed.
#
#     If the environment variable NOBENCH is set, benchmarking is not
#  performed (and OUTPUT has no effect).  This allows a program that
#  links ibench to run in the ordinary way.
#
############################################################################

global Save__, Saves__, Name__, Labels__

# List information before running.
#
procedure Init__(prog)
   if getenv("NOBENCH") then {	# don't do benchmarking
      Term__ := 1
      return
      }
   Name__ := prog			# program name
   Labels__ := ["total ","static","string","block "]
   write(Name__,": benchmarking\n")
   Signature__()			# initial information
   Regions__()
   Time__()
   if not getenv("OUTPUT") then {	# if OUTPUT is set, allow output
      Save__ := write			# turn off output
      Saves__ := writes
      write := writes := -1
      }
   else write(Name__,": output\n")
   return
end

# List information at termination.

procedure Term__()
   if not getenv("OUTPUT") then {	# if OUTPUT is not set, restore output
      write := Save__
      writes := Saves__
      }
					# final information
   Regions__()
   Storage__()
   Collections__()
   Allocated__()
   write("\n",Name__,": elapsed time = ",Time__()," ms.")
   return
end

#
# List total amounts of allocation.  Needs Icon Version 8.5 or above.
#
procedure Allocated__()
   local allocated

   allocated := []
   every put(allocated,&allocated)
   Display__(allocated,"allocated")
   return

end

# List garbage collections performed.
#
procedure Collections__()
   local collections

   collections := []
   every put(collections,&collections)
   Display__(collections,"collections")
   return
end

# List region sizes.
#
procedure Regions__()
   local regions, count
   
   regions := []
   every put(regions,&regions)
   count := 0
   every count +:= !regions
   push(regions,count)
   Display__(regions,"regions")
   return
end

# List relveant implementation information
#
procedure Signature__()

   every write(&version | &host | &features)
   return

end

# List storage used.
#
procedure Storage__()
   local storage, count
   
   storage := []
   every put(storage,&storage)
   count := 0
   every count +:= !storage
   push(storage,count)
   Display__(storage,"storage")
   return
end

# List elapsed time.
#
procedure Time__()
   static lasttime

   initial lasttime := &time
   return &time - lasttime
end

# Display storage information
#
procedure Display__(data,name)
   local i

   write("\n",name,":\n")
   every i := 1 to *Labels__ do
      write(Labels__[i],right(data[i],8))
end