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
|
#
#
# Nimrod's Runtime Library
# (c) Copyright 2008 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
# This file implements the Nimrod profiler. The profiler needs support by the
# code generator.
type
TProfileData {.compilerproc, final.} = object
procname: cstring
total: float
var
profileData {.compilerproc.}: array [0..64*1024-1, TProfileData]
proc sortProfile(a: var array[0..64*1024-1, TProfileData], N: int) =
# we use shellsort here; fast enough and simple
var h = 1
while true:
h = 3 * h + 1
if h > N: break
while true:
h = h div 3
for i in countup(h, N - 1):
var v = a[i]
var j = i
while a[j-h].total <= v.total:
a[j] = a[j-h]
j = j-h
if j < h: break
a[j] = v
if h == 1: break
proc writeProfile() {.noconv.} =
const filename = "profile_results"
var i = 0
var f: TFile
var j = 1
while openFile(f, filename & $j & ".txt"):
closeFile(f)
inc(j)
if openFile(f, filename & $j & ".txt", fmWrite):
var N = 0
# we have to compute the actual length of the array:
while profileData[N].procname != nil: inc(N)
sortProfile(profileData, N)
writeln(f, "total running time of each proc" &
" (interpret these numbers relatively)")
while profileData[i].procname != nil:
write(f, profileData[i].procname)
write(f, ": ")
writeln(f, profileData[i].total)
inc(i)
closeFile(f)
addQuitProc(writeProfile)
|