home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap04 / 04vbu02 / profiler.bas < prev   
Encoding:
BASIC Source File  |  1995-07-03  |  2.6 KB  |  122 lines

  1. Attribute VB_Name = "profiler"
  2. Option Explicit
  3.  
  4. #If PROFILING Then
  5.     Const MAX_SECONDS = 86400   ' number of seconds at midnight
  6.     
  7.     Dim iFileHandle As Integer  ' handle for the output file
  8.     Dim sngTimers(20) As Single ' storage for 20 simultaneous timers
  9.     Dim iLastTimer As Integer   ' last entry in the array used
  10. #End If
  11.  
  12. Function MarkEndTime(sTag As String) As Integer
  13.  
  14. #If PROFILING Then
  15.     On Error GoTo Error_MarkEndTime
  16.     
  17.     Dim sngEndTime As Single, sngDifference As Single
  18.     
  19.         ' record the end time and adjust for the passage of midnight
  20.     sngEndTime = Timer
  21.     If sngEndTime < sngTimers(iLastTimer) Then sngEndTime = sngEndTime + MAX_SECONDS
  22.     
  23.         ' compute the elapsed time
  24.     sngDifference = sngEndTime - sngTimers(iLastTimer)
  25.     
  26.         ' write it to a file, with a number of space that reflect
  27.         ' its place in the stack of timers.
  28.     Print #iFileHandle, Spc(iLastTimer); sTag; " "; sngDifference
  29.  
  30.         ' free an array entry for re-use
  31.     iLastTimer = iLastTimer - 1
  32.     
  33.     MarkEndTime = True
  34.     Exit Function
  35.     
  36. Error_MarkEndTime:
  37.     MarkEndTime = False
  38.     Exit Function
  39.     
  40. #Else
  41.     MarkEndTime = True
  42.     Exit Function
  43. #End If
  44.     
  45. End Function
  46.  
  47. Function MarkStartTime() As Integer
  48.  
  49. #If PROFILING Then
  50.     On Error GoTo Error_MarkStartTime
  51.     
  52.     Dim i As Integer
  53.     
  54.         ' get the next entry to use
  55.     i = iLastTimer + 1
  56.     
  57.         ' record the starting time value
  58.     sngTimers(i) = Timer
  59.     iLastTimer = i
  60.     
  61.     MarkStartTime = True
  62.     Exit Function
  63.     
  64. Error_MarkStartTime:
  65.     MarkStartTime = False
  66.     Exit Function
  67.  
  68. #Else
  69.     MarkStartTime = True
  70.     Exit Function
  71. #End If
  72.     
  73. End Function
  74.  
  75. Function StopProfiler() As Integer
  76.  
  77. #If PROFILING Then
  78.     On Error GoTo Error_StopProfiler
  79.     
  80.         ' close the output file.
  81.     Close #iFileHandle
  82.     StopProfiler = True
  83.     Exit Function
  84.     
  85. Error_StopProfiler:
  86.     StopProfiler = False
  87.     Exit Function
  88.     
  89. #Else
  90.     StopProfiler = True
  91.     Exit Function
  92. #End If
  93.     
  94. End Function
  95.  
  96. Function StartProfiler(sFileName As String) As Integer
  97.  
  98. #If PROFILING Then
  99.     On Error GoTo Error_StartProfiler
  100.  
  101.         ' open an output file for the timing information
  102.     iFileHandle = FreeFile
  103.     Open sFileName For Output As #iFileHandle
  104.     
  105.         ' initialize the counters
  106.     iLastTimer = 0
  107.     
  108.     StartProfiler = True
  109.     Exit Function
  110.  
  111. Error_StartProfiler:
  112.     StartProfiler = False
  113.     Exit Function
  114.  
  115. #Else
  116.     StartProfiler = True
  117.     Exit Function
  118. #End If
  119.     
  120. End Function
  121.  
  122.