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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cache properties in variables

Status
Not open for further replies.

Cowper

Programmer
Sep 18, 2002
29
AU
Hi,
I am reading a VB certificate exam book for my upcoming exam. There is one question about optimising an application that I don't understand and needs your help. The book says one tip for increasing performance is to cache properties in variables when you are using a loop. The reason is it is faster to access data stored in a variable them to access data stored in a property.

I still don't understand how it works. Could someone explain a bit more on this subject?

Many thanks

Cowper

 
Sure

every.time.that.vb.hits.a.dot it has to dereference the property value that follows (6 dereferences in the opening phrase of this sentence). This is a relatively slow process. As a one-off it may not be a big performance hit, but if you had a 5000 iteration loop, it would be 30000 (6*5000) slow operations.

By dereferencing the whole lot once into a variable prior to entering the loop, as in:

Wombat=every.time.that.vb.hits.a.dot

you avoid that overhead. Another alternative is to use the With construct, which achieves the same objective
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top