Ooops
January 20, 2021, 01:21:07 PM

Author Topic: [bmx] TProfiler Class by AntonyWells [ 1+ years ago ]  (Read 422 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] TProfiler Class by AntonyWells [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : TProfiler Class
Author : AntonyWells
Posted : 1+ years ago

Description : Profile your functions to determine bottlenecks in your code.

Example

Code: [Select]
Import "Profiler.bmx" ' This is the code archive entry below

function Test()
   TProfiler.Enter("Test")
   for j=1 to 10000000
    local k = sin(j)*cos(j)+atan(sin(2),cos(2))
 next
  TPRofiler.Leave("Test")
end function




Create a unique enter leave name for each function.

You can also isolate and profile specific chunks of code by placing enter and leave at the top and bottom of the intended target.

On program exit the class automatically saves a profiler.txt file. it does this using an onEnd hook. [/i]

Code :
Code: BlitzMax
  1. Strict
  2. Type TLength
  3.         Field time:Int
  4. End Type
  5.  
  6. Type TCall
  7.        
  8.         Field name:String
  9.         Field start
  10.         Field Times:TList
  11.         Field calls
  12.         Method New()
  13.                 times = CreateList()
  14.         End Method
  15.                
  16. End Type
  17.  
  18. tprofiler.calls = CreateList()
  19. Type TProfiler
  20.  
  21.         Global calls:TList
  22.         Function DumpLog( file:String )
  23.                
  24.                 Local fi:TStream = WriteFile( file )
  25.                
  26.                         WriteLine fi,"Aurora Profiler Log V1.0"
  27.                         For Local c:TCall = EachIn calls
  28.                                
  29.                                 WriteLine fi,"----------------------------"
  30.                                 Local totTime=0
  31.                                 For Local t:TLength = EachIn c.times
  32.                                         totTime:+t.time
  33.                                 Next
  34.                                 WriteLine fi,"Function:"+C.name+" Calls:"+c.calls+" Total:"+TotTime+" Avg:"+Float(TotTime)/Float(c.calls)
  35.                                 WriteLine fi,"Total (Seconds):"+String( Float(tottime)/Float(1000) )
  36.                                 WriteLine fi,"Avg (Seconds):"+String( (Float(TotTime)/Float(c.calls) ) / Float(1000) )
  37.                         Next
  38.                        
  39.                        
  40.                 CloseFile fi   
  41.                
  42.         End Function
  43.                        
  44.         Function Enter( func:String )
  45.                
  46.                 For Local call:tcall = EachIn calls
  47.        
  48.                         If call.name = func
  49.                                        
  50.                                 call.start = MilliSecs()
  51.                                 call.calls:+1
  52.                                 Return
  53.                                
  54.                         EndIf
  55.  
  56.                 Next
  57.                
  58.                 Local call:TCall = New tcall
  59.                 calls.addlast( call )
  60.                 call.calls = 1
  61.                 call.name = func
  62.                 call.start = MilliSecs()
  63.                        
  64.         End Function
  65.  
  66.         Function Leave( func:String )
  67.                
  68.                 For Local call:Tcall = EachIn calls
  69.                        
  70.                         If call.name = func
  71.                                
  72.                                 Local l:TLength = New tlength
  73.                                 l.time = MilliSecs()-call.start
  74.                                 call.times.addlast( l )
  75.                                 Return
  76.                                
  77.                         End If
  78.                        
  79.                 Next
  80.                
  81.                 RuntimeError "Unknown function"
  82.                
  83.         End Function
  84.  
  85. End Type
  86. OnEnd( EndHook )
  87. Function EndHook()
  88.  
  89.         Print "Dumping profile information."
  90.         TProfiler.DumpLog("Profiler.txt")
  91.         Print "Dumped."
  92.        
  93. End Function


Comments :


Damien Sturdy(Posted 1+ years ago)

 This is really useful actually, GenevaTheGenWhatsis.


AntonyWells(Posted 1+ years ago)

 Yes it is, CyngusTheSingingDetective


AntonyWells(Posted 1+ years ago)

 Yes it is, CyngusTheSingingDetective


fredborg(Posted 1+ years ago)

 Small handy addition:
Code: [Select]
Import "Profiler.bmx" ' This is the code archive entry below

function Test()
  ?debug
   TProfiler.Enter("Test")
  ?
   for j=1 to 10000000
    local k = sin(j)*cos(j)+atan(sin(2),cos(2))
 next
  ?debug
  TPRofiler.Leave("Test")
  ?
end function



plash(Posted 1+ years ago)

 or you could go further and do..
Code: [Select]
Function Enter( func:String )
?debug
For Local call:tcall = EachIn calls

If call.name = func

call.start = MilliSecs()
call.calls:+1
Return

EndIf

Next

Local call:TCall = New tcall
calls.addlast( call )
call.calls = 1
call.name = func
call.start = MilliSecs()
?
End Function

Function Leave( func:String )
       ?debug
For Local call:Tcall = EachIn calls

If call.name = func

Local l:TLength = New tlength
l.time = MilliSecs()-call.start
call.times.addlast( l )
Return

End If

Next

RuntimeError "Unknown function"
?
End Function
though the app would still be wasting time by even calling the function.. unless max is smart enough to know that its completely useless in release mode.. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal