Visual Basic .NET Forums  

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-07-2009, 9:13 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Jan 2009
Age: 35
Posts: 13
Reputation: 9
mad_schatz is on a distinguished programming path ahead
Default Access Faster in Binary reading Single Data Types

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
Unfortunately this reading process takes long time. About 4 mins.

My question is, Is there any other way to read single data types from a binary file more faster ?

Thanks for all
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-07-2009, 10:51 AM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Posts: 712
Reputation: 369
MattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalistMattP VB.NET gold medalist
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-07-2009, 11:14 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

Quote:
Originally Posted by mad_schatz View Post
Unfortunately this reading process takes long time. About 4 mins.
Eek!

ReDim Preserve laCoords(liArrayLen) would be your problem



Every time you read one number, you re-dimension the array to make it one larger. Problem is, to re-dim an array VB must create a new array and copy all the elements to the new one. Given that you re-dim it 300,000 plus times, and the average array size is 150,000 elements, that means 45 billion copy operations of 4 bytes of data (which may be stored in an 8 byte memory location) and youre talkling about shifting around somewhere between 180 and 360 gigabytes of information, just to read a 150kb file!

Stop using array, and stop using Redim Preserve. Use a data storage container that supports your method of reading, and also what you plan to do with the data afterwards. At the very least, work out from the file size, how many singles it is likely to contain and pre-dim an array of the appropriate size
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-07-2009, 11:51 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Jan 2009
Age: 35
Posts: 13
Reputation: 9
mad_schatz is on a distinguished programming path ahead
Thumbs up

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-08-2009, 9:09 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

it sthe same way for strings.. If youre doing a LOT of string concatenation you can end up moving gigabytes around. Use a Stringbuilder to avoid similar performance hits
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-09-2009, 12:51 AM
jmcilhinney's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Aug 2004
Location: Sydney, Australia
Age: 40
Posts: 5,309
Reputation: 415
jmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NETjmcilhinney master of VB.NET
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.
__________________
Essential: Multiple Forms
101 Samples: 2002 | 2003 | 2005Free Components: WFC | XPCC | ElementsEx | VBPP | ADO.NET/MySQL | VisualStyles | NPlot | SDFTutorials: Home & Learn | Start VB.NET | Learn VB.NETFavourites: MSDN | WinForms.NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-09-2009, 7:28 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.0 (VS 2005/2008)
 
Join Date: Jan 2009
Age: 35
Posts: 13
Reputation: 9
mad_schatz is on a distinguished programming path ahead
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-09-2009, 12:39 PM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

There are other collections, Stack, Queue, Dictionary ... Read up on them all and see the differences. Each situation calls for a different container
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 6:27 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0


For advertising opportunities click here.