We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 12,793 times

Related Categories

Make LBound() and UBound() even more efficient

In a previous tip, we showed you how to loop through an array with
the LBound() and UBound() function. The code we provided used these
two functions in the For...Next statement itself, like this:

For x = LBound(MusicGenres) To UBound(MusicGenres)

where MusicGenres was the array we wanted to traverse.

As many astute tipsters pointed out, howevever, when you loop
through large arrays it's much more efficient to set variables to the
LBound() and UBound() function results and use these variables inside
the loop instead. This way, Visual Basic only wastes time performing
the functions once rather than many times.

So the code would then become

lctr = LBound(MusicGenres)
uctr = UBound(MusicGenres)
For x = lctr To uctr
    Debug.Print MusicGenres(x)
Next x

© 2001 Element K Journals, a division of Element K Press LLC ("Element K"). Element K and the Element K logo are trademarks of Element K LLC

Comments

  • Compiler Optimised

    Posted by goatless on 15 Jan 2004

    Just to confirm the previous poster, For loop variables are automatically record by the compiler on the first iteration.

    While and Do Loops are re-evaluated every time and will benefit from turni...

  • Is this necessary ?

    Posted by bchow on 21 Mar 2002

    Really, doesn't the VB Interpreter take care of this for you ?
    I used to work on C++ compilers and the optimizer will create this substitution for you.