Re: Framework 2.0 array redim unsatisfactory performance
- From: "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxx>
- Date: Mon, 8 Aug 2005 21:47:44 -0500
Tom,
| Does anyone
| have any idea whether this will be addressed in the final release?
No, it will not be addressed per se. As each time you do the ReDim Preserve
a brand new array is defined & every element is copied from the existing
array into the new array!
NOTE: Long in VB. NET is a 640Bit value, I suspect you mean Integer which is
now 32-bit (as compared to VB6's Long which was 32-bit). If you are using
Long thinking you have 32-bit values you are actually copying & using twice
as much memory as you need to!
I would recommend instead of ReDim Preserve, that you consider using
System.Collections.Generic.List(Of T) instead, as it will dynamically
allocate memory on a as needed bases (it starts with a buffer of 16
elements, then doubles it each time it needs more space. The List(Of
T).Count reflects the number of elements currently in the collection, while
List(Of T).Capacity reflects the number of elements that can be in the
collection (in other words the size of the buffer). When Count reaches
Capacity the buffer is increased...
For details on List(Of T) see:
http://msdn2.microsoft.com/library/6sh2ey19(en-us,vs.80).aspx
Something like (from memory):
Dim foos As List(Of Integer)
Dim n As Integer = 100000
For index As Integer = 1 To n
foos.Add(index)
Next
NOTE: In .NET arrays & collections use base 0 indexing instead of base 1
indexing. Which means that foos(0) = 1.
NOTE: The "double the size of the buffer" is the general algorithm that .NET
1.0 & 1.1 use, .NET 2.0 may or may not change the algorithm to increase the
size of the buffer...
Hope this helps
Jay
"Tom Jastrzebski" <tom@xxxxxxx> wrote in message
news:EOTJe.781$FV1.299@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
| Hello,
|
| I was just testing VB.Net on Framework.Net 2.0 performance when I run into
| the this problem.
| This trivial code attached below executed hundreds, if not thousand times
| faster in VB 6.0 than in .Net environment, under VS 2005 Beta 2. Does
anyone
| have any idea whether this will be addressed in the final release?
|
| Thanks,
| Tomasz
|
| Public Sub Main()
| Dim foos() As Long
|
| Dim n As Long
| n = 100000
| Dim i As Long
| For i = 1 To n
| ReDim Preserve foos(i)
| foos(i) = i
| Next i
| Debug.Print(foos(1000))
| End Sub
|
|
.
- Follow-Ups:
- Re: Framework 2.0 array redim unsatisfactory performance
- From: Jay B. Harlow [MVP - Outlook]
- Re: Framework 2.0 array redim unsatisfactory performance
- From: Tom Jastrzebski
- Re: Framework 2.0 array redim unsatisfactory performance
- References:
- Framework 2.0 array redim unsatisfactory performance
- From: Tom Jastrzebski
- Framework 2.0 array redim unsatisfactory performance
- Prev by Date: Re: Manual scroll rendering
- Next by Date: How do you determine the owner of a process
- Previous by thread: Framework 2.0 array redim unsatisfactory performance
- Next by thread: Re: Framework 2.0 array redim unsatisfactory performance
- Index(es):
Relevant Pages
|