Efficiency of References

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Efficiency of References

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:20 pm

by Torrie Moore >> Fri, 14 Jan 2000 0:10:17 GMT

Hi

We have a defaults object that contains several object references for the status of a transaction. If I want to check if the status of a tracsaction is one of several statuses eg

foreach trans in allTrans do
...
if trans.myStatus = defaults.myDefaultStatus1
or trans.myStatus = defaults.myDefaultStatus2
or trans.myStatus = defaults.myDefaultStatus3 then
...
endif
...
endforeach

Is it more efficient to use the code above or the code below? The code below implies that the status objects are pulled accross to the client node. Does this also happen in the above example.

vars
status1, status2, status3 : Status;
begin

...
status1 := defaults.myDefaultStatus1
status2 := defaults.myDefaultStatus2
status3 := defaults.myDefaultStatus3

foreach trans in allTrans do
if trans.myStatus = status1
or trans.myStatus = status2
or trans.myStatus = status3 then
...
endif
...
endforeach


Torrie Moore
Concept Engineering Limited
email torrie@concept-eng.co.nz
phone +64 3 343 3362
fax +64 3 343 3364
Snail mail PO Box 514, Christchurch, New Zealand

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Efficiency of References

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:21 pm

by Craig Shearer >> Fri, 14 Jan 2000 2:13:41 GMT

Hi Torrie

In both your examples, the code won't cause the status objects to be loaded into cache because you are only looking at the object reference. Only if you call a method on the status object, or get one of its properties will it be fetched from the server.

Also, if this code executes a lot, then it is likely that the status objects would be in cache, thus avoiding the trip to the server anyway.

However, there is a difference between the two code fragments in that the second is slightly more efficient as you're caching the reference to the status objects in a local variable rather than using your "defaults" object and getting a property off that each time. Whether you'd actually notice the difference or not would really depend on the number of iterations through the loop, but in my mind, it's usually better to code efficiently in the first place as long as the code is clear. As a bonus, I believe that the second code is easier to read than the first anyway.

Note that you could make the code even more efficient by doing the following:


vars
status1, status2, status3 : Status;
transStatus : Status;
begin

...
status1 := defaults.myDefaultStatus1
status2 := defaults.myDefaultStatus2
status3 := defaults.myDefaultStatus3

foreach trans in allTrans do
transStatus := trans.myStatus;

if transStatus = status1
or transStatus = status2
or transStatus = status3 then
...
endif
...
endforeach


In the above code, you can then cache the value of trans.myStatus in a local variable instead of referencing it off the transaction object for each comparison in the if statement.

Craig.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 12 guests