Ok - I used a link from MartinLiss about timing items - here's the code I used.
My goal was to compare finding the number of days in a month for two dates - a January date - which the leap year means nothing about and a Feburary date - which the leap year logic does involve.Code:Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Form_Load() ' Place this code in any Sub or Function Dim lngStart As Long Dim lngFinish As Long Dim lngCounterOne As Long Dim lngCounterTwo As Long Dim lngMonthCount As Long Dim Months(1 To 12) As Long Dim lngMonth As Long Dim lngYear As Long Dim mco As Long 'Dim ld As Date Dim CheckDate1 As Date Dim CheckDate2 As Date Dim strDate1 As String Dim strDate2 As String CheckDate1 = "2004-01-15" CheckDate2 = "2004-02-15" ' Record the start "time" lngStart = GetTickCount() ' Some process that you want to time For lngCounterOne = 1 To 100000 For lngCounterTwo = 1 To 5 lngMonthCount = Day(DateSerial(Year(CheckDate1), Month(CheckDate1) + 1, 0)) lngMonthCount = Day(DateSerial(Year(CheckDate2), Month(CheckDate2) + 1, 0)) Next lngCounterTwo Next lngCounterOne ' Record the finish "time" lngFinish = GetTickCount() ' Display the difference Debug.Print "Time to complete DATE method = "; lngFinish - lngStart Months(1) = 31 Months(2) = 29 Months(3) = 31 Months(4) = 30 Months(5) = 31 Months(6) = 30 Months(7) = 31 Months(8) = 31 Months(9) = 30 Months(10) = 31 Months(11) = 30 Months(12) = 31 strDate1 = "01152004" strDate2 = "02152004" ' Record the start "time" lngStart = GetTickCount() ' Some process that you want to time For lngCounterOne = 1 To 100000 For lngCounterTwo = 1 To 5 lngMonth = CLng(Mid$(strDate1, 1, 2)) lngYear = CLng(Right$(strDate1, 4)) If lngMonth = 2 Then If lngYear / 100 = lngYear \ 100 Then ' we have century If lngYear / 400 = lngYear \ 400 Then ' and a leap lngYear mco = 0 Else mco = 1 End If Else If lngYear / 4 = lngYear \ 4 Then ' not on century and we have a leap lngYear mco = 0 Else mco = 1 End If End If Else mco = 0 End If lngMonthCount = Months(lngMonth) - mco lngMonth = CLng(Mid$(strDate2, 1, 2)) lngYear = CLng(Right$(strDate2, 4)) If lngMonth = 2 Then If lngYear / 100 = lngYear \ 100 Then ' we have century If lngYear / 400 = lngYear \ 400 Then ' and a leap lngYear mco = 0 Else mco = 1 End If Else If lngYear / 4 = lngYear \ 4 Then ' not on century and we have a leap lngYear mco = 0 Else mco = 1 End If End If Else mco = 0 End If lngMonthCount = Months(lngMonth) - mco Next lngCounterTwo Next lngCounterOne ' Record the finish "time" lngFinish = GetTickCount() ' Display the difference Debug.Print "Time to complete Logic/Math method = "; lngFinish - lngStart End Sub
The immediate window showed that the method I posted (logic and math without date functions) ran in nearly half the time.
Hopefully I didn't make some silly blunder in comparing these two - it's been a long day...
Code:Time to complete DATE method = 4562 Time to complete Logic/Math method = 2625




Reply With Quote