If order is not critical, you can use:

For i = ubound(arr) downto 0
...
Next

In that case, there is no variable, and ubound is calculated only once
(just as if you would have used a variable).

UBound is a function, I doubt they recalculate the size every time,
but there is definitely function calling overhead.

Math

On 5/17/07, Daniel Stenning <[EMAIL PROTECTED]> wrote:
> When I have to use for/next for processing arrays,
> I like, I suspect many others, tend to put the array ubound into a local
> variable like so:
>
> Dim counter as integer = myarray.ubound // ( new rb syntax )
>
> For I as integer = 0 to counter
>   ...
> Next
>
>
> As opposed to , what would be more clean:
>
>
> For I as integer = 0 to myarray.ubound
>   ...
> Next
>
>
> Now I just did some timings, and it seems that there is a slight ( not great
> ) performance advantage with using a local variable. I used the following
> code ( with an empty array )
>
> //-------
>   dim u as integer = UBound(arr)
>
>   Result.Text = "Started"
>   app.DoEvents
>
>   for j as integer = 0 to 200000000
>     for i as integer = 0 to u
>     Next
>   Next
>
>
>   Beep
>   Result.Text= "FINISHED"
> //--------
>
>
> and:
>
> // -------
>   Result.Text = "Started"
>   app.DoEvents
>
>   for j as integer = 0 to 200000000
>     for i as integer = 0 to  UBound(arr)
>     Next
>   Next
>
>   Beep
>   Result.Text= "FINISHED"
> //--------
>
>
> This makes me wonder: -  Idoes Ubound use a method/function call to get the
> ubound value,  or does the RB generate code to directly access a "property"
> or variable holding the internal array size ?.
>
>
> Naturally there would be some speed advantage to NOT using any function to
> get the size.
>
>
> Basically I would prefer not to have to use the local variable counter
> method, or  as I often have done - creating a class property to hold the
> array count.  It would be so much cleaner just to use UBOUND directly in the
> for loop, knowing that I incur no performance penalty for doing so.
>
>
> Anyone have info on this ?  My test seems to suggest there is not a huge
> difference in speed, but a difference nevertheless.
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to