![]() |
Click here to advertise with us
|
|
|||||||
| VB.NET General Discussion VB.NET general discussion area |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi Gents,
I'm new in VB.Net and I'm trying to learn it. I've a binary file which has 300.000+ single floating point numbers which I'm reading with the following code : Code:
Dim ReadStream As FileStream
Dim llFileLen As Long
Dim liArrayLen As Integer
Dim lcFileName As String
liArrayLen = 0
lcFileName = Application.StartupPath & "\World_new.dat"
Me.TextBox1.Text = Now
ReadStream = New FileStream(lcFileName, FileMode.Open)
Dim readBinary As New BinaryReader(ReadStream)
llFileLen = ReadStream.Length
Do While ReadStream.Position < llFileLen
ReDim Preserve laCoords(liArrayLen)
laCoords(liArrayLen) = readBinary.ReadSingle()
liArrayLen = liArrayLen + 1
Loop
Me.TextBox2.Text = Now
readBinary = Nothing
ReadStream = Nothing
My question is, Is there any other way to read single data types from a binary file more faster ? Thanks for all |
|
|||
|
I wrote 300,000 single values to a binary file for testing. Reading the contents into a List(Of Single) took about 5 seconds on my system (low end Core 2 Duo).
Code:
Dim singleCollection As New List(Of Single)
Using reader As New BinaryReader(File.OpenRead("C:\Temp\BinaryFile.dat"))
Do Until reader.PeekChar() = -1
singleCollection.Add(reader.ReadSingle())
Loop
End Using
|
|
|||
|
Hi All,
Thank you for your replies Mattp and cjard. Cjard, you're right about array. As soon as I stop re-dimming it, problem solved. I did never thought that it could use that much information. the numbers are floating numbers which helps me to plot a simple world map on a drawing surface. It is not something serious. I'm just playing around with vb.net in order to learn. Thank you very much for your helps gents. |
|
||||
|
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) 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.
__________________
![]() 2007, 2008, 2009, 2010 Why is my data not saved to my database? | Communicating between multiple forms MSDN: Data Walkthroughs | "How Do I?" Videos My Blog: Custom Events | Dynamic GDI+ Drawing |
|
|||
|
I really appreciate your explanations. Thank you once more.
As I'm very new in vb.net, I don't know how to do things in better way. I'll try to use LIST learn its' power. Thanks |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|