![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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. 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. 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 Specs![]() |
| | #3 (permalink) |
| | 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. 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. 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 > 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 Specs![]() |
| | #6 (permalink) |
| | 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) 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 Specs![]() |
| | #7 (permalink) |
| | 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 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 Specs![]() |
![]() |
| 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 | |||