<font face=Courier New>
' ZVI:2009-07-25 http://www.mrexcel.com/forum/showthread.php?t=404266
Sub Test_IsEven_Methods()
Dim t1#, t2#, t3#, i&, ret As Boolean
Const N = 100000 ' <-- Amount of the test cycles
Const TestNum = 2.5 ' <-- The testing value
On Error Resume Next ' <-- Not available method(s) will be skipped
' Method 1 - Evaluate("ISEVEN(Fix(2.5))")
' Fix() or Str() should be used instead of CStr() for correct working in all localization
t1 = Timer
For i = 1 To N
'ret = Evaluate("ISEVEN(" & Str(TestNum) & ")")
ret = Evaluate("ISEVEN(" & Fix(TestNum) & ")")
Next
t1 = Timer - t1
If t1 = 0 Then t1 = 0.001 ' exclude DIV/0 error for low N
If Err <> 0 Then
t1 = 0
Debug.Print "1) ATP Excel - Evaluate(""ISEVEN(Fix(" & TestNum & "))""):" _
, "Not available" & vbLf
Else
Debug.Print "1) ATP Excel - Evaluate(""ISEVEN(Fix(" & TestNum & "))""): " & vbLf _
& Format(t1, "0.000") & " sec" _
, Format(N / t1, "# ### ##0") & " op/sec" _
, "ret = " & ret _
, vbLf
End If
' Method 2 - Application.IsEven(2)
Err.Clear
t2 = Timer
For i = 1 To N
ret = Application.IsEven(TestNum)
Next
t2 = Timer - t2
If t2 = 0 Then t2 = 0.001 ' exclude DIV/0 error for low N
If Err <> 0 Then
t2 = 0
Debug.Print "2) ATP Excel2007-Application.IsEven(" & TestNum & "):" _
, "Not available" & vbLf
Else
Debug.Print "2) ATP Excel2007 - Application.IsEven(" & TestNum & "):" & vbLf _
& Format(t2, "0.000") & " sec" _
, Format(N / t2, "# ### ##0") & " op/sec" _
, "ret = " & ret _
, vbLf
End If
' Suggested VBA method 3 - IsEven(2)
t3 = Timer
For i = 1 To N
ret = IsEven(TestNum)
Next
t3 = Timer - t3
If t3 = 0 Then t3 = 0.001 ' exclude DIV/0 error for low N
Debug.Print "3) VBA - IsEven(" & TestNum & "): " & vbLf _
& Format(t3, "0.000") & " sec" _
, Format(N / t3, "# ### ##0") & " op/sec" _
, "ret = " & ret _
, vbLf
On Error GoTo 0
' Ratio
Debug.Print String(60, "-")
Debug.Print "Speed ratios of the methods:"
If t1 = 0 Then
Debug.Print "1) Analysis ToolPack not found"
Else
Debug.Print "1) Evaluate(""IsEven()"")", , "ratio = 1"
End If
If t2 = 0 Then
Debug.Print "2) Excel 2007 not found"
Else
Debug.Print "2) Application.IsEven()", , "ratio = " & Format(t1 / t2, "0")
End If
Debug.Print "3) VBA IsEven()", , "ratio = " & Format(t1 / t3, "0")
Debug.Print String(60, "-")
End Sub
Function IsEven(Num As Double) As Boolean
IsEven = Fix(Num) / 2 - Fix(Fix(Num) / 2) = 0
End Function</FONT>