|
|
Модуль для сравнения скорости работы Процедур (Функций)
Давно уже пользуюсь подобным , наглядно, и сразу видно какой из двух (3х-4х...) вариантов аналогичного кода быстрее работает.
Модуль:
Private Sub SpeedCompare()
Const сiTotTests As Integer = 4
Const сlngIterations As Long = 1000
Dim intTestNo As Integer
Dim lngIteration As Long
Dim strTestNote As String
Dim strTestName As String
Dim sglTimerStart As Single, sglTimerEnd As Single
Dim vVal, sReport$
On Error GoTo SpeedCompare_Err
Screen.MousePointer = 11
strTestNote = "Demo v008"
sReport = "Тест из " & Format$(сlngIterations, "#,##0") & " повторов" & _
IIf(Len(strTestNote) > 0, " (" & strTestNote & ")", "") & ":"
Debug.Print sReport
For intTestNo = 1 To сiTotTests
Select Case intTestNo
Case 1: strTestName = "Round() Function"
Case 2: strTestName = "Format() Function"
Case 3: strTestName = "Fix() Function"
Case 4: strTestName = "Int() Function"
End Select
sglTimerStart = Timer
For lngIteration = 1 To сlngIterations
Select Case intTestNo
Case 1
vVal = Round(lngIteration * 23.56, 3)
Case 2
vVal = Format(lngIteration * 21.33, "# ##0.00")
Case 3
vVal = Fix(lngIteration * 23.56)
Case 4
vVal = Int(lngIteration * 23.56)
End Select
Next lngIteration
sglTimerEnd = Timer
vVal = ElapsedTimeInSec(sglTimerStart, sglTimerEnd)
sReport = Format(intTestNo, "00") & ". "
sReport = sReport & strTestName & String(54 - Len(strTestName), ".")
sReport = sReport & " Продолжительность: " & vVal
Debug.Print sReport
Next intTestNo
Debug.Print String(90, "-") & "/"
SpeedCompare_End:
Screen.MousePointer = 1
Exit Sub
SpeedCompare_Err:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub :" & _
"SpeedCompare - mod01SpeedTest.", vbCritical, "Error!"
Resume SpeedCompare_End
End Sub
Private Function ElapsedTimeInSec(sglTimerStart As Single, Optional ByVal sglTimerEnd!, _
Optional blnLongFormat As Boolean) As String
Dim sglTookSeconds!, datTookDays As Date, sglMS As Single
Const csglSecondsPerDay As Single = 86400
If sglTimerEnd = 0 Then sglTimerEnd = Timer
If sglTimerEnd < sglTimerStart Then
sglTookSeconds = (csglSecondsPerDay - sglTimerStart) + sglTimerEnd
Else
sglTookSeconds = sglTimerEnd - sglTimerStart
End If
If blnLongFormat = False Then
ElapsedTimeInSec = Format(sglTookSeconds, "#,##0.000") & " сек."
Else
datTookDays = DateAdd("s", sglTookSeconds, Date)
sglMS = sglTookSeconds - DatePart("s", datTookDays)
ElapsedTimeInSec = Format$(datTookDays, "hh:nn:ss") & "." & Format$(sglMS * 1000, "0")
End If
End Function
|
|