Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Fast copy method of sub array (=array range) possible?

Reply
 
Old 03-19-2009   #1 (permalink)
Thomas Lebrecht


 
 

Fast copy method of sub array (=array range) possible?

Assume I want to copy the sub-array with the slots 234 until 789 from arrayone
to the end of arraytwo I can do this step by step with

ind = 234
do until (ind = 790)
arraytwo.Add arrayone(ind)
ind = ind + 1
Loop

But this can be rather inefficient.
I could imagine that there is a faster way with copying a whole sub array at once.
Somethig similar to:

arraytwo.Add arrayone.getSubArray(234,789)

The same question arises for delete operation.

Thomas


My System SpecsSystem Spec
Old 03-19-2009   #2 (permalink)
Andrew Morton


 
 

Re: Fast copy method of sub array (=array range) possible?

Thomas Lebrecht wrote:
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
Why? Somewhere something has to do the loop, be it in your code or the code
generated from your code.

Maybe it's just your preference to use a do...loop, but do you think a
for...next loop would be neater?

And are you talking about arrays or arraylists? The help for my VS2008 says
that Array.Add always throws an exception because Arrays are of fixed size
and so cannot be added to.
Quote:

> I could imagine that there is a faster way with copying a whole sub
> array at once. Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)

ArrayList.AddRange and ArrayList.GetRange appear to be what you want.
Quote:

> The same question arises for delete operation.
ArrayList.RemoveRange

Also, depending on your version of VS, you might want to consider using a
List(Of <type>) because they are more efficient as the computer "knows" in
advance what sort of object it is dealing with in the List.

HTH

Andrew


My System SpecsSystem Spec
Old 03-19-2009   #3 (permalink)
Andrew Morton


 
 

Re: Fast copy method of sub array (=array range) possible?

My reply doesn't seem to have propagated correctly, so I'm posting it again
through a different news server...

Thomas Lebrecht wrote:
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
Why? Somewhere something has to do the loop, be it in your code or the code
generated from your code.

Maybe it's just your preference to use a do...loop, but do you think a
for...next loop would be neater?

And are you talking about arrays or arraylists? The help for my VS2008 says
that Array.Add always throws an exception because Arrays are of fixed size
and so cannot be added to.
Quote:

> I could imagine that there is a faster way with copying a whole sub
> array at once. Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)

ArrayList.AddRange and ArrayList.GetRange appear to be what you want.
Quote:

> The same question arises for delete operation.
ArrayList.RemoveRange

Also, depending on your version of VS, you might want to consider using a
List(Of <type>) because they are more efficient as the computer "knows" in
advance what sort of object it is dealing with in the List.

HTH

Andrew


My System SpecsSystem Spec
Old 03-19-2009   #4 (permalink)
Cor Ligthert[MVP]


 
 

Re: Fast copy method of sub array (=array range) possible?


Thomas,

What do you call inefficient, 3 picoseconds or 5 picoseconds?

Don't take to much time with these kind of methods. For sure takes one move
on the screen from your form 100 times more time.

Although the For index is more common currently.

Cor


"Thomas Lebrecht" <t.lebrecht@xxxxxx> wrote in message
news:49c2071e$0$32669$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
> I could imagine that there is a faster way with copying a whole sub array
> at once.
> Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)
>
> The same question arises for delete operation.
>
> Thomas
>
My System SpecsSystem Spec
Old 03-19-2009   #5 (permalink)
Pegasus [MVP]


 
 

Re: Fast copy method of sub array (=array range) possible?


"Thomas Lebrecht" <t.lebrecht@xxxxxx> wrote in message
news:49c2071e$0$32669$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
> I could imagine that there is a faster way with copying a whole sub array
> at once.
> Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)
>
> The same question arises for delete operation.
>
> Thomas
>
The OP's question is entirely reasonable. From a computing-time point of
view it is usually much more efficient to deal with the elements of an array
with compiled and optimized code than on an element-by-element basis at the
source language level. Depending on the array size, we're not talking about
picoseconds as Core Lightert suggests but about milliseconds. That's one
million times longer!

The following example illustrates the principle. It consists of three
components:
1. Populate an array of 100,000 elements with random numbers.
2. Copy this array as a whole to a second array and report the time it
takes.
3. Copy this array on an element-by-element basis to a third array and
report the time it takes.

On my machine the element-by-element copy takes 16 times longer than the
direct copy. This is a huge improvement that no programmer should ignore.
Unfortunately I am not aware of how Step 2 above can be performed on a
sub-array.

const size = 100000
ReDim a1(size)
ReDim a3(size)

For i = 0 To UBound(a1)
a1(i) = Rnd
Next

t = Timer
a2 = a1
WScript.Echo "Elapsed time=" & (Timer - t) * 1000 & " ms"

t = Timer
For i = 0 To UBound(a1)
a3(i) = a1(i)
Next
WScript.Echo "Elapsed time=" & (Timer - t) * 1000 & " ms"


My System SpecsSystem Spec
Old 03-19-2009   #6 (permalink)
Herfried K. Wagner [MVP]


 
 

Re: Fast copy method of sub array (=array range) possible?

"Thomas Lebrecht" <t.lebrecht@xxxxxx> schrieb:
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
> I could imagine that there is a faster way with copying a whole sub array
> at once.
> Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)
Are you actually using arrays or a collection/list type?

Just take a look at 'Array.Copy', 'Array.CopyTo' and maybe
'Buffer.BlockCopy' if you are dealing with arrays.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

My System SpecsSystem Spec
Old 03-19-2009   #7 (permalink)
Hal Rosser


 
 

Re: Fast copy method of sub array (=array range) possible?


"Thomas Lebrecht" <t.lebrecht@xxxxxx> wrote in message
news:49c2071e$0$32669$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to copy the sub-array with the slots 234 until 789 from
> arrayone
> to the end of arraytwo I can do this step by step with
>
> ind = 234
> do until (ind = 790)
> arraytwo.Add arrayone(ind)
> ind = ind + 1
> Loop
>
> But this can be rather inefficient.
> I could imagine that there is a faster way with copying a whole sub array
> at once.
> Somethig similar to:
>
> arraytwo.Add arrayone.getSubArray(234,789)
>
> The same question arises for delete operation.
>
> Thomas
In the Array class, one of the methods is:
Public Shared Sub Copy ( _
sourceArray As Array, _
sourceIndex As Long, _
destinationArray As Array, _
destinationIndex As Long, _
length As Long _
)

Visual Basic (Usage)
Dim sourceArray As Array
Dim sourceIndex As Long
Dim destinationArray As Array
Dim destinationIndex As Long
Dim length As Long

Array.Copy(sourceArray, sourceIndex, _
destinationArray, destinationIndex, _
length)

this will do what you want.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to create array without quotes? $array = (a,b,c) PowerShell
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
Fast byte array comparison for ordering purposes .NET General
how to assign values to array and how to create array via variable PowerShell
Question about passing array to a .Net method PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46