G
Guest
I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays to
add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteArray". It was interesting that using the memorystream was
twice as fast as the ArrayList and almost 2000 time as fast as using normal
byte array with redimensions. Thought some might be interested in this.
Private Sub TestBuildByteArraySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte
'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cb As ByteArray = New ByteArray(startsize)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ticks - starttime
'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cl As ArrayList = New ArrayList(startsize)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.ToArray(GetType(Byte)), Byte())
timeArrayList = DateTime.Now.Ticks - starttime
'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cs As ArrayStream = New ArrayStream(500)
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ticks - starttime
End Sub
End Class
Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class
Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(size)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb)
End Sub
Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub
Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize()
End Sub
End Class
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays to
add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteArray". It was interesting that using the memorystream was
twice as fast as the ArrayList and almost 2000 time as fast as using normal
byte array with redimensions. Thought some might be interested in this.
Private Sub TestBuildByteArraySpeed(ByRef timeByteArray As Long, ByRef
timeArrayList As Long, ByRef TimeArrayStream As Long)
'Testing speed off byte array building
'Notes: Add in Finalize to close and release memory
Dim starttime As Long
Dim count As Integer = 2000
Dim startsize As Integer = 500
Dim i, j As Integer
Dim byt As Byte = 125
Dim nb As Byte() = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
Dim b() As Byte
'Test ByteArray Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cb As ByteArray = New ByteArray(startsize)
For i = 1 To count
For j = 0 To 50 : cb.Add(byt) : Next
cb.AddRange(nb)
Next
b = cb.ToArray
timeByteArray = DateTime.Now.Ticks - starttime
'Test ArrayList Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cl As ArrayList = New ArrayList(startsize)
For i = 1 To count
For j = 0 To 50 : cl.Add(byt) : Next
cl.AddRange(nb)
Next
b = DirectCast(cl.ToArray(GetType(Byte)), Byte())
timeArrayList = DateTime.Now.Ticks - starttime
'Test ArrayStream Class
ReDim b(0)
starttime = DateTime.Now.Ticks
Dim cs As ArrayStream = New ArrayStream(500)
For i = 1 To count
For j = 0 To 50 : cs.Add(byt) : Next
cs.AddRange(nb)
Next
b = cs.ToArray
TimeArrayStream = DateTime.Now.Ticks - starttime
End Sub
End Class
Public Class ByteArray
Private u, c, i As Integer
Private b As Byte()
Public Sub New(ByVal size As Integer)
MyBase.New()
ReDim b(size)
u = size
c = 0
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
Return b
End Get
End Property
Public Sub Add(ByVal nb As Byte)
If c > u Then ReDim Preserve b(c) : u = c
b(c) = nb
c = c + 1
End Sub
Public Sub AddRange(ByVal nb() As Byte)
i = c + UBound(nb, 1)
If i > u Then ReDim Preserve b(i)
nb.CopyTo(b, c)
c = i
End Sub
End Class
Public Class ArrayStream
Private ms As MemoryStream
Public Sub New(ByVal size As Integer)
MyBase.New()
ms = New MemoryStream(size)
End Sub
Public ReadOnly Property ToArray() As Byte()
Get
ms.Position = 0
Return ms.ToArray()
End Get
End Property
Public Sub Add(ByVal nb As Byte)
ms.WriteByte(nb)
End Sub
Public Sub AddRange(ByVal nb() As Byte)
ms.Write(nb, 0, UBound(nb) + 1)
End Sub
Protected Overrides Sub finalize()
ms.Close()
MyBase.Finalize()
End Sub
End Class