[AccessD] Closure on the String to Date topic

Salakhetdinov Shamil mcp2004 at mail.ru
Tue Nov 27 11:50:34 CST 2012


Hi Arthur and Stuart,

Here is a bit quicker version of EoM :)

Public Function EoM2(dt As Date)
EoM2 = DateSerial(Year(dt), Month(dt) + 1, 0)
End FunctionIf a bit more seriously what's the use of such one line function(s)? - as you can find from the test results below almost 25% of time is spent to pass parameter(s) and to get return value. Of course that are just microseconds but imagine you have a lot of such "one-liners" and they are called in cycles etc.

Thank you.

-- Shamil

P.S. Tests results:

EoM: Elapsed time = 0.140625
EoM2: Elapsed time = 0.125
Inline: DateSerial(Year(dt), Month(dt) + 1, 0): Elapsed time = 0.09375P.P.S. Test code:

Option Compare Database
Public Sub Test()
Const MAX_COUNT As Long = 100000
Dim startTime As Single
Dim endTime As Single
Dim counter As Long
Dim testName As String
Dim result As Date
Dim dt As Date
dt = Now
' EoM
testName = "EoM"
startTime = Timer
For counter = 1 To MAX_COUNT
result = EoM(dt)
Next counter
endTime = Timer
Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
' EoM2
testName = "EoM2"
startTime = Timer
For counter = 1 To MAX_COUNT
result = EoM2(dt)
Next counter
endTime = Timer
Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
' Inline: DateSerial(Year(dt), Month(dt) + 1, 0)
testName = "Inline: DateSerial(Year(dt), Month(dt) + 1, 0)"
startTime = Timer
For counter = 1 To MAX_COUNT
result = DateSerial(Year(dt), Month(dt) + 1, 0)
Next counter
endTime = Timer
Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
End Sub
Public Function EoM(dt As Date)
EoM = DateSerial(Year(dt), Month(dt) + 1, 1) - 1
End Function
Public Function EoM2(dt As Date)
EoM2 = DateSerial(Year(dt), Month(dt) + 1, 0)
End Function


Tue 27 Nov 2012 00:08:11 от Arthur Fuller <fuller.artful at gmail.com>:
>	
>
>
	
	
>
		
		
			
>There we go. Thanks!
>

>

>
On Mon, Nov 26, 2012 at 11:27 PM, Stuart McLachlan
>
<stuart at lexacorp.com.pg>wrote:
>

>
> Alternative (simpler?)  functions:
>
>
>
> Public Function BoM(dt As Date) As Date
>
>     BoM = DateSerial(year(dt),month(dt),1)
>
> End Function
>
>
>
> Public Function BoY(dt As Date) As Date
>
>      BoY = DateSerial(year(dt),1,1)
>
>  End Function
>
>
>
> Public Function EoY(dt As Date) As Date
>
>      EoY = Dateserial(year(dt),12,31)
>
> End Function
>
>
>
> Public Function EoM(dt as date)
>
>   EOM =  DateSerial(year(dt),month(dt) +1,1) -1
>
> End Function
>
>
>
>
><<< skipped >>>


More information about the AccessD mailing list