This puts a Profile in more readable order. (Lots of PB'ers contributed to this.)
Comments here: http://www.powerbasic.com/support/pb...968#post301968
Comments here: http://www.powerbasic.com/support/pb...968#post301968
Code:
' ' Profile_Ordered 'Reorders and reformats the file Created by the "Profile" command 'This Output is "Willie Sutton Style" (robbed from many PB'ers) 'Instructions to install: ' ''''Just before PBMain (outside of Functions/subs) put these 2 Macros and 1 Type ' Macro Profile_Print_Array If InStr(n$, "Alpha") Then Array Sort p_Info(), Ascend 'Names Else Array Sort p_Info(), Descend 'Numbers so start with largest first End If RSet u1$ = "*** " & n$ & " ***" 'sorted name Print #1 'blank line Print #1, u1$ & " Calls Tot ms ms/call % of time" Reset ctr For i = LBound(p_Info()) To UBound(p_Info()) If p_Info(i).Calls > 0 Then 'no sense showing these Incr ctr RSet u2$ = Trim$(p_Info(i).Name) 'Procedure name Print #1, Using$("###) ", ctr); _ 'counter u2$; _ 'Name Using$(" ###,###,###", p_Info(i).Calls); _ 'calls to procedure Using$(" #,###,###,###", p_Info(i).Time_Used); _ 'time spent there Using$(" #,###.#", p_Info(i).Time_Used / p_Info(i).Calls); _ 'avg per call Using$(" ##% ", p_Info(i).Time_Used / ttl_secs * 100) 'time spent here as part of program run End If Next i End Macro ' Macro Profile_Top_Header Open Profile_FileName For Output As #1 ' hrs = Profile_Start_Time \ 3600 mins = (Profile_Start_Time - (hrs * 3600)) \ 60 secs = (Profile_Start_Time - (hrs * 3600)) - (mins * 60) ' Local t1, t2 As String t1$ = Using$("##:##:##", hrs, mins, secs) t2$ = Time$ Replace " " With "0" In t1$ '0's instead of spaces Replace " " With "0" In t2$ ' "" ' Print #1,, "Profile for " & Profile_FileName Print #1,, Date$ & " Ran from " & _ t1$ & " to " & _ t2$ & _ Using$(" for #, seconds", Timer - Profile_Start_Time) Print #1,, "(Note elapsed seconds may not agree with ms Total due " Print #1,, "to the different methods used to calculate each." Print #1,, " Especially on shorter runs - say under 2 minutes)" End Macro ' Type Profile_Type_Info Sort_Field As String * 100 Name As String * 50 'way plenty Calls As Long Time_Used As Long End Type ' ''''Inside PBMain the top put the next 2 lines ' Local Profile_Start_Time as long ' Profile_Start_Time = timer ''''At the end of PBMain put this line: ' Profile_Ordered(Profile_Start_Time) 'Applikashunfurstunkendun ' Function Profile_Ordered(Profile_Start_Time As Long) As Long Local ttl_secs, Longest, ctr, i, k, del, hrs, mins, secs As Long Local P_info() As Profile_Type_Info Local sfile, Profile_FileName, n, u, u1, u2 As String Profile_FileName = CurDir$ & "\" & _ 'Exe.Path$ only returns 8 char Dos names so use CurDir$ Exe.Name$ & _ 'Progran running name "_Profile.txt" 'Add a unique identifier to it ' Profile Profile_FileName 'create the file ' retry: Try Open Profile_FileName For Binary As #1 Catch 'Error so not ready to open yet Sleep 100 'take a nap GoTo retry 'try again End Try ' sFile = Space$(Lof(#1)) 'make string to hold file Get #1,, sFile 'put te file in the string Close 'don't need it anymore ' ctr = ParseCount( sFile, $CrLf ) 'get number of lines Local temp$() ReDim temp$(1 To ctr)' to hold lines Parse sFile, Temp$(), $CrLf 'put lines in here ReDim P_info(ctr) 'get out Type ready ' 'Parse Temp$() into Type Fields For ctr = LBound(temp$()) To UBound(temp$()) p_Info(ctr).Name = Parse$(temp$(ctr), 1) p_Info(ctr).Name = MCase$(p_Info(ctr).Name)'cleaner looking p_Info(ctr).Calls = Val(Parse$(temp$(ctr), 2)) p_Info(ctr).Time_Used = Val(Parse$(temp$(ctr), 3)) ttl_Secs = Ttl_Secs + p_Info(ctr).Time_Used Next ctr ' 'formatting strings u1$ = Space$(30) 'for header string u2$ = Space$(25) 'for procedure name ' Profile_Top_Header 'Print Top of page ' n$ = "Alphabetical" For ctr = LBound(p_Info()) To UBound(p_Info()) p_Info(ctr).Sort_Field = p_Info(ctr).Name 'sort by name Next ctr Profile_Print_Array 'print results ' n$ = "Call Frequency" For ctr = LBound(p_Info()) To UBound(p_Info()) p_Info(ctr).Sort_Field = Using$("#########.######", p_Info(ctr).Calls) & _ p_Info(ctr).Name Next ctr Profile_Print_Array ' n$ = "Time Used" For ctr = LBound(p_Info()) To UBound(p_Info()) p_Info(ctr).Sort_Field = Using$("#########.######", p_Info(ctr).Time_Used) & _'sort by Calls p_Info(ctr).Name Next ctr Profile_Print_Array ' ' n$ = "UnCalled For" 'put in Alpha order For ctr = LBound(p_Info()) To UBound(p_Info()) p_Info(ctr).Sort_Field = Using$("#########.######", p_Info(ctr).Calls) & _ p_Info(ctr).Name Next ctr Array Sort p_info() Reset i Print #1, " " Print #1, " *** Uncalled Procedures ***" For ctr = LBound(p_Info()) To UBound(p_Info()) If p_Info(ctr).Calls < 1 Then Incr i Print #1, Using$("###) ", i) & p_Info(ctr).Name End If Next ctr ' ' Print #1, " " Print #1,, "**** The format courtesy of many PB Programmers ****" Close #1 ' End Function '***********/Profile_Ordered
Comment