Maybe you could do the test:
dim foo(1048576) as Integer
dim U as Integer = UBound(foo)
Dim c as integer
// time ubound on full array
for i as Integer = 0 to U
C = UBound(foo)
next
Redim foo(-1)
// time ubound on empty array
for i as Integer = 0 to U
C = UBound(foo)
next
This test would verify once for all whether the time taken to do UBOUND
varies according to the number of elements
( my strong hunch would be that it makes no difference since internally RB
is just checking an internal size variable )
On 17/5/07 16:11, "Charles Yeomans" <charles at declareSub dot com> wrote:
> 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>
>
Regards,
Dan
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|