realbasic-nug
[Top] [All Lists]

Re: For/next and array ubounds: optimising performance

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: For/next and array ubounds: optimising performance
From: Charles Yeomans <charles at declareSub dot com>
Date: Thu, 17 May 2007 11:11:37 -0400
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <BAY107-DAV2B6C18E84C64A6300D58993330 at phx dot gbl>
I just did a quick test myself.


Consider the following loops.


dim foo(1048576) as Integer
for i as Integer = 0 to UBound(foo)
next


dim foo(1048576) as Integer
dim U as Integer = UBound(foo)
for i as Integer = 0 to U
next


The first loop takes about 20000 microseconds, while the second loop  
takes about 14000 ms (with #pragma disableBackgroundTasks, etc.).   
Some further simple testing suggests that the cost of each evaluation  
UBound is about .007 microseconds.  So if your code inside the loop  
takes, say, 1 microsecond to execute, you can achieve a speedup of  
0.7 percent.  Usually, I'll opt for keeping the code a little simpler.

Charles Yeomans


On May 17, 2007, at 10:45 AM, Daniel Stenning wrote:

> Are you saying that RB Ubound does an iterating  count of the  
> number of
> elements each time ??
>
>  I would have thought that each RB array has an internal count  
> "property"
> that only gets modified as elements are added or removed, and this  
> property
> is what gets queried each time Ubound is called.  I might be wrong  
> but that
> would seem the most efficient thing to do - at the cost of a single  
> integer.
>
>
> On 17/5/07 15:30, "James Sentman" <james at sentman dot com> wrote:
>
>>
>> The problem is that you're evaluating the ubound everytime you go
>> around the loop. The higher the ubound value the more times you'll
>> have to evaluate it and the slower your loop will run.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

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


<Prev in Thread] Current Thread [Next in Thread>