View Single Post
  #6 (permalink)  
Old 01-09-2009, 12:51 AM
jmcilhinney's Avatar
jmcilhinney jmcilhinney is offline
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 6,123
Reputation: 541
jmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalistjmcilhinney VB.NET gold medalist
Default

As cjard says, use a data structure that's designed for what you're doing. In your case that would be a List(Of Single). A List is like an array in many ways but it will allow you to add and remove items dynamically, growing and shrinking as required.

Internally, the List uses an array and it will work similarly to what you're already doing in that, as the array gets filled, the system will create a new, larger array and copy the existing data to it. The difference is that the List will not increase the size of the array by just 1 each time. Whenever it needs to grow it will double the size of the array.

By default, initially there is no array. When you add the first item an array with a Length of 4 is created. Each time you try to add an item beyond the Length of the array the size is doubled, so it goes 0, 4, 8, 16, 32, 64, 128, 256, etc. You should be able to see that that will lead to far fewer reallocations than if you grow an array by one element each time. This provides a good trade-off between keeping the array size as small as possible and minimising the amount of reallocation.

Furthermore, if you know that your List is going to nd up being large then you can specify an initial size for the internal array. If you know for a fact that your file will contain at least 300,000 entries then you should specify 300,000 as the initial capacity:
Code:
Dim numbers As New List(Of Single)(300000)
That way your List will never have to reallocate until the 300,001st item is added. The capacity will then grow to 600,000 so another reallocation will not be needed for quite some time.

If you don't know with any sort of accuracy how many items there will be then just make a guesstimate that balances the desire to increase the capacity as few times as possible with the desire not to make the internal array much bigger than is necessary. Once you've done adding items you can force one final reallocation to shrink the internal array to the exact size needed by calling the List's TrimExcess method.
Reply With Quote