Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Warning Ugly Technical Question ;) 1

Status
Not open for further replies.

oscarse

Programmer
Apr 9, 2006
99
CA
Does anyone know if adding DIM statements inside an IF statement is of any benifit to the speed of an Application?

In other words; Would these 2 examples produce the same compile/token code in ACCESS VBA?

DIM blah As <Object>
IF Foo = TRUE THEN
' do some work
END IF

VS

IF Foo = TRUE THEN
DIM blah As <Object>
' Do some Work
End If

if the Variable/Object is not created until inside the IF statement (read Memory Allocated) then the code should theoretically run faster ... unless the compiler has already allocated the memory within the Token code and just providing a local pointer to the Variable/object. I seem to recall that was the way DBase worked.

Anyway be interested to here other comments.
 
The Dim instruction is (AFAIK) compile time, not run time.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Given PHV's info, wouldn't putting DIM inside the loop result in late-binding, which would actually decrease performance?

Silence is golden.
Duct tape is silver.
 
I can see the DIM automatically allocating variable space at compile time ... but not so clear on Objects ... given a DAO object, is all the info available at compile time to be able to allocate the appropriate size of the object.

The compiler is most likely making an allocation for a far pointer for objects but somewhere sometime the object must be allocated. I could envision that there may be some runtime allocation although it still doesn't have to be done in the module but rather when an app starts.

Still leaves me wondering if the object definition is in the body of code whether it will make any difference in speed of execution ... perhaps a couple benchmark programs are needed to resolve this question.

ie make 10000 calls to one sub/func that declares an object at the beginning and 10000 calls to one that declares an object in the body of code ... timimg each should reveal the answere
 
Well PHV is king!!!

8 secounds to execute both examples SO NO DIFFERENCE

Thanx for the responses.

Option Compare Database
Option Explicit

Public Function test1()
ginPNID = 0
Dim i As Long
Debug.Print "test1"; Time
For i = 1 To 10000
Call ttt1
Next i
Debug.Print "test1-"; Time
End Function
Public Function test2()
ginPNID = 0
Dim i As Long
Debug.Print "test2"; Time
For i = 1 To 10000
Call ttt2
Next i
Debug.Print "test2-"; Time
End Function
Public Sub ttt1()
Dim dbo As DAO.Database
Dim rs As DAO.Recordset
Set dbo = CurrentDb
Set rs = dbo_OpenRecordset("PhoneNumbers", dbOpenDynaset)
If ginPNID = 0 Then
rs.FindFirst "[PNID] LIKE '*'"
End If

End Sub
Public Sub ttt2()
If ginPNID = 0 Then
Dim dbo As DAO.Database
Dim rs As DAO.Recordset
Set dbo = CurrentDb
Set rs = dbo_OpenRecordset("PhoneNumbers", dbOpenDynaset)
rs.FindFirst "[PNID] LIKE '*'"
End If

End Sub
 
Just looked at the code ... ttt1 and 2 should have been

If ginPNID <> 0 Then

In this case test2 runs in < 1 second ... however its not the DIM statement that is allocating the memory its the SET statements so PHV's answer is still correct.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top