Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Access vs Random Access VB.Net vs VB 6.0

Status
Not open for further replies.

donutman

Programmer
Dec 20, 2002
2,481
US
Using random access in VB.Net has serious performance penalties, so consider using file streams with binary reader & writer. Here's a performance comparison.

Module Module1
Structure EmployeeRec
<VBFixedString(20)> Public Name As String 'not much point in fixing the length of string unless using FileGet and FilePut
Public ID As Int16
Public Pay As Single
End Structure
Sub Main()
Dim recEE As New EmployeeRec()
Dim sFileNameOut As String = &quot;c:\test.Out&quot;
Dim btBuffer(100) As Byte 'dimension > needed
Dim iLen As Int16
Dim lCnt As Long
Dim iFileNum As Int16
Dim dDuration As Date
Dim fs As IO.FileStream = IO.File.Create(sFileNameOut)
Dim br As New IO.BinaryReader(fs)
Dim bw As New IO.BinaryWriter(fs)
Dim encoder As New System.Text.ASCIIEncoding()
With recEE
'write the record using binary writer
.Name = &quot;Karl&quot;
.ID = 10
.Pay = 1001
encoder.GetBytes(LSet(.Name, 20), 0, 20, btBuffer, 0)
fs.Write(btBuffer, 0, 20) 'use the file stream to read and write strings compatible with VB < 7.0
bw.Write(.ID) 'use binary reader to read and write all other data types
bw.Write(.Pay) 'the binary writer writes strings with a length descriptor attached
.Name = &quot;&quot;
.ID = 0
.Pay = 0

'read the record with binary reader
dDuration = Now
For lCnt = 1 To 1000000
fs.Seek(0, IO.SeekOrigin.Begin)
fs.Read(btBuffer, 0, 20)
.Name = encoder.GetString(btBuffer, 0, 20)
.ID = br.ReadInt16
.Pay = br.ReadSingle
Next
lCnt = DateDiff(DateInterval.Second, dDuration, Now)
fs.Close()
br.Close()
bw.Close()
MsgBox(&quot;Binary access duration is &quot; & lCnt & &quot; seconds.&quot;)
MsgBox(.Name & &quot; &quot; & .ID.ToString & &quot; &quot; & .Pay.ToString)
End With
'read and write using random file access method
iFileNum = FreeFile()
FileOpen(iFileNum, sFileNameOut, OpenMode.Random, OpenAccess.ReadWrite)
With recEE
.Name = &quot;Karl&quot;
.ID = 12
.Pay = 201
End With
FilePut(iFileNum, recEE, 1)
dDuration = Now
For lCnt = 1 To 1000000
FileGet(iFileNum, recEE, 1)
Next
lCnt = DateDiff(DateInterval.Second, dDuration, Now) 'the IDE doesn't always automatically correct the capitalization of varibles that are dimed within if then block
MsgBox(&quot;Radom access duration is &quot; & lCnt & &quot; seconds.&quot;)
MsgBox(recEE.Name & &quot; &quot; & recEE.ID.ToString & &quot; &quot; & recEE.Pay.ToString)
FileClose(iFileNum)
End Sub

End Module

The result is 4 seconds for binary access and 64 seconds for random access. BTW in VB 6.0 a similar program runs in 2 seconds using RANDOM ACCESS!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top