Building Byte Arrays - Speed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis

    Building Byte Arrays - Speed

    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
    "TestBuildByteA rray". 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 TestBuildByteAr raySpeed(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.Ti cks
    Dim cb As ByteArray = New ByteArray(start size)
    For i = 1 To count
    For j = 0 To 50 : cb.Add(byt) : Next
    cb.AddRange(nb)
    Next
    b = cb.ToArray
    timeByteArray = DateTime.Now.Ti cks - starttime

    'Test ArrayList Class
    ReDim b(0)
    starttime = DateTime.Now.Ti cks
    Dim cl As ArrayList = New ArrayList(start size)
    For i = 1 To count
    For j = 0 To 50 : cl.Add(byt) : Next
    cl.AddRange(nb)
    Next
    b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
    timeArrayList = DateTime.Now.Ti cks - starttime

    'Test ArrayStream Class
    ReDim b(0)
    starttime = DateTime.Now.Ti cks
    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.Ti cks - 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(si ze)
    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

    --
    Dennis in Houston
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Building Byte Arrays - Speed

    Dennis,
    Your ByteArray is so slow as you are recreating the array each time you add
    a byte.

    Consider doing what ArrayList & MemoryStream do, over allocate the buffer.

    Instead of having the buffer contain the exact number of bytes in it, have
    it contain Capicity # of bytes, then have a second variable for the Length
    number of bytes.

    I posted the start of a ByteBuffer class that functions very similar to the
    System.Text.Str ingBuffer class in that it maintains a byte array internally
    allowing you to append byte arrays on the end, expanding the internal array
    as needed.



    Hope this helps
    Jay

    "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
    news:FAD79C2A-122C-4F0A-B193-8BB45F2E8941@mi crosoft.com...[color=blue]
    > 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[/color]
    to[color=blue]
    > 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
    > "TestBuildByteA rray". It was interesting that using the memorystream was
    > twice as fast as the ArrayList and almost 2000 time as fast as using[/color]
    normal[color=blue]
    > byte array with redimensions. Thought some might be interested in this.
    >
    > Private Sub TestBuildByteAr raySpeed(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,[/color]
    11,[color=blue]
    > 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.Ti cks
    > Dim cb As ByteArray = New ByteArray(start size)
    > For i = 1 To count
    > For j = 0 To 50 : cb.Add(byt) : Next
    > cb.AddRange(nb)
    > Next
    > b = cb.ToArray
    > timeByteArray = DateTime.Now.Ti cks - starttime
    >
    > 'Test ArrayList Class
    > ReDim b(0)
    > starttime = DateTime.Now.Ti cks
    > Dim cl As ArrayList = New ArrayList(start size)
    > For i = 1 To count
    > For j = 0 To 50 : cl.Add(byt) : Next
    > cl.AddRange(nb)
    > Next
    > b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
    > timeArrayList = DateTime.Now.Ti cks - starttime
    >
    > 'Test ArrayStream Class
    > ReDim b(0)
    > starttime = DateTime.Now.Ti cks
    > 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.Ti cks - 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(si ze)
    > 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
    >
    > --
    > Dennis in Houston[/color]


    Comment

    • Dennis

      #3
      Re: Building Byte Arrays - Speed

      I did find a couple of errors in the ByteArray class but it didn't make any
      difference in speed. I also tried using an initial allocation sufficently
      large so the only redim needed was at the end to make the byte array the
      correct size. However, in real life, one can't avoid the redim if one is
      dealing with variable inputs from users. The ArrayList was still twice as
      fast as the ByteArray class and the MemoryStream class was twice as fast as
      the ArrayList. My main point is that the Memorystream is quite fast for
      simple byte array manipulation.

      Haven't had a chance to look at your ByteBuffer yet but will tonight.


      Private Sub TestBuildByteAr raySpeed(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 = 1000
      Dim startsize As Integer = 80000
      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.Ti cks
      Dim cb As ByteArray = New ByteArray(start size)
      For i = 1 To count
      For j = 0 To 50 : cb.Add(byt) : Next
      cb.AddRange(nb)
      Next
      b = cb.ToArray
      timeByteArray = DateTime.Now.Ti cks - starttime

      'Test ArrayList Class
      ReDim b(0)
      starttime = DateTime.Now.Ti cks
      Dim cl As ArrayList = New ArrayList(start size)
      For i = 1 To count
      For j = 0 To 50 : cl.Add(byt) : Next
      cl.AddRange(nb)
      Next
      b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
      timeArrayList = DateTime.Now.Ti cks - starttime

      'Test ArrayStream Class
      ReDim b(0)
      starttime = DateTime.Now.Ti cks
      Dim cs As ArrayStream = New ArrayStream(sta rtsize)
      For i = 1 To count
      For j = 0 To 50 : cs.Add(byt) : Next
      cs.AddRange(nb)
      Next
      b = cs.ToArray
      TimeArrayStream = DateTime.Now.Ti cks - 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
      ReDim Preserve b(c - 1)
      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) : u = i
      nb.CopyTo(b, c)
      c = i + 1
      End Sub
      End Class

      Public Class ArrayStream
      Private ms As MemoryStream
      Public Sub New(ByVal size As Integer)
      MyBase.New()
      ms = New MemoryStream(si ze)
      End Sub
      Public ReadOnly Property ToArray() As Byte()
      Get
      ms.SetLength(ms .Position)
      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


      "Jay B. Harlow [MVP - Outlook]" wrote:
      [color=blue]
      > Dennis,
      > Your ByteArray is so slow as you are recreating the array each time you add
      > a byte.
      >
      > Consider doing what ArrayList & MemoryStream do, over allocate the buffer.
      >
      > Instead of having the buffer contain the exact number of bytes in it, have
      > it contain Capicity # of bytes, then have a second variable for the Length
      > number of bytes.
      >
      > I posted the start of a ByteBuffer class that functions very similar to the
      > System.Text.Str ingBuffer class in that it maintains a byte array internally
      > allowing you to append byte arrays on the end, expanding the internal array
      > as needed.
      >
      > http://groups.google.com/groups?q=By...phx.gbl&rnum=3
      >
      > Hope this helps
      > Jay
      >
      > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
      > news:FAD79C2A-122C-4F0A-B193-8BB45F2E8941@mi crosoft.com...[color=green]
      > > 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[/color]
      > to[color=green]
      > > 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
      > > "TestBuildByteA rray". It was interesting that using the memorystream was
      > > twice as fast as the ArrayList and almost 2000 time as fast as using[/color]
      > normal[color=green]
      > > byte array with redimensions. Thought some might be interested in this.
      > >
      > > Private Sub TestBuildByteAr raySpeed(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,[/color]
      > 11,[color=green]
      > > 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.Ti cks
      > > Dim cb As ByteArray = New ByteArray(start size)
      > > For i = 1 To count
      > > For j = 0 To 50 : cb.Add(byt) : Next
      > > cb.AddRange(nb)
      > > Next
      > > b = cb.ToArray
      > > timeByteArray = DateTime.Now.Ti cks - starttime
      > >
      > > 'Test ArrayList Class
      > > ReDim b(0)
      > > starttime = DateTime.Now.Ti cks
      > > Dim cl As ArrayList = New ArrayList(start size)
      > > For i = 1 To count
      > > For j = 0 To 50 : cl.Add(byt) : Next
      > > cl.AddRange(nb)
      > > Next
      > > b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
      > > timeArrayList = DateTime.Now.Ti cks - starttime
      > >
      > > 'Test ArrayStream Class
      > > ReDim b(0)
      > > starttime = DateTime.Now.Ti cks
      > > 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.Ti cks - 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(si ze)
      > > 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
      > >
      > > --
      > > Dennis in Houston[/color]
      >
      >
      >[/color]

      Comment

      • Cor Ligthert

        #4
        Re: Building Byte Arrays - Speed

        Dennis,

        I think (mostly I try this, I did not because I use for this kind of things
        mostly the memorystream) that when you first calculate the length of your
        byteArray that you can speed that part up by avoiding the redim.

        Cor



        Comment

        • Dennis

          #5
          Re: Building Byte Arrays - Speed

          Yes, it speeds it up by hundreds of times. However, my problem was that I
          don't know the final size of my byte array to begin with since I was building
          it from several user's input of strings and I had no idea of the number nor
          the size of the strings the users would input. I mainly was just amazed that
          the memorystream was so fast.

          "Cor Ligthert" wrote:
          [color=blue]
          > Dennis,
          >
          > I think (mostly I try this, I did not because I use for this kind of things
          > mostly the memorystream) that when you first calculate the length of your
          > byteArray that you can speed that part up by avoiding the redim.
          >
          > Cor
          >
          >
          >
          >[/color]

          Comment

          • Cor Ligthert

            #6
            Re: Building Byte Arrays - Speed

            Dennis,

            I was not clear, I did mean, first calculating the size of the array and
            after that starts filling, maybe it need more time, however I think that
            that will be earned not doing the redims.

            Cor


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Building Byte Arrays - Speed

              Dennis,
              What ArrayList & StringBuilder does is double its buffer each time the
              buffer is not large enough to add any more elements. The buffer starts out
              at 16 elements. I believe MemoryStream follows the same double the buffer
              rule (its a good simple rule to use).

              In your original code & this code you were adding 1 to the buffer each time
              the buffer was not large enough, which is each time you added an element
              (once past the initial size).

              What I am suggesting is you start with at least a 16 element buffer, then in
              your add method if the buffer does not have enough free room double it. As
              you add elements you will be increasing the buffer less & less.

              Its also a good idea in your class as well as MemoryStream, ArrayList &
              StringBuilder to give it a good initial estimated capacity. For example, if
              you expect to add 1000 elements to the class, then start with a capacity of
              1000.
              [color=blue]
              > the ArrayList. My main point is that the Memorystream is quite fast for
              > simple byte array manipulation.[/color]
              My main point is that the MemoryStream is holding an Array of Bytes in
              memory, when it needs to reallocate this array of bytes is effectively does
              an ReDim Preserve!

              My second point is if the MemoryStream is doing too much you can build your
              own ByteArray that will perform equally well! By "too much" I mean there are
              properties & methods on MemoryStream that you don't need such as Seek.

              Hope this helps
              Jay


              "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
              news:8E8AC8D3-22BC-414C-8A1D-B2A7BC6144B4@mi crosoft.com...[color=blue]
              > I did find a couple of errors in the ByteArray class but it didn't make[/color]
              any[color=blue]
              > difference in speed. I also tried using an initial allocation sufficently
              > large so the only redim needed was at the end to make the byte array the
              > correct size. However, in real life, one can't avoid the redim if one is
              > dealing with variable inputs from users. The ArrayList was still twice as
              > fast as the ByteArray class and the MemoryStream class was twice as fast[/color]
              as[color=blue]
              > the ArrayList. My main point is that the Memorystream is quite fast for
              > simple byte array manipulation.
              >
              > Haven't had a chance to look at your ByteBuffer yet but will tonight.
              >
              >
              > Private Sub TestBuildByteAr raySpeed(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 = 1000
              > Dim startsize As Integer = 80000
              > 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,[/color]
              11,[color=blue]
              > 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.Ti cks
              > Dim cb As ByteArray = New ByteArray(start size)
              > For i = 1 To count
              > For j = 0 To 50 : cb.Add(byt) : Next
              > cb.AddRange(nb)
              > Next
              > b = cb.ToArray
              > timeByteArray = DateTime.Now.Ti cks - starttime
              >
              > 'Test ArrayList Class
              > ReDim b(0)
              > starttime = DateTime.Now.Ti cks
              > Dim cl As ArrayList = New ArrayList(start size)
              > For i = 1 To count
              > For j = 0 To 50 : cl.Add(byt) : Next
              > cl.AddRange(nb)
              > Next
              > b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
              > timeArrayList = DateTime.Now.Ti cks - starttime
              >
              > 'Test ArrayStream Class
              > ReDim b(0)
              > starttime = DateTime.Now.Ti cks
              > Dim cs As ArrayStream = New ArrayStream(sta rtsize)
              > For i = 1 To count
              > For j = 0 To 50 : cs.Add(byt) : Next
              > cs.AddRange(nb)
              > Next
              > b = cs.ToArray
              > TimeArrayStream = DateTime.Now.Ti cks - 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
              > ReDim Preserve b(c - 1)
              > 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) : u = i
              > nb.CopyTo(b, c)
              > c = i + 1
              > End Sub
              > End Class
              >
              > Public Class ArrayStream
              > Private ms As MemoryStream
              > Public Sub New(ByVal size As Integer)
              > MyBase.New()
              > ms = New MemoryStream(si ze)
              > End Sub
              > Public ReadOnly Property ToArray() As Byte()
              > Get
              > ms.SetLength(ms .Position)
              > 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
              >
              >
              > "Jay B. Harlow [MVP - Outlook]" wrote:
              >[color=green]
              > > Dennis,
              > > Your ByteArray is so slow as you are recreating the array each time you[/color][/color]
              add[color=blue][color=green]
              > > a byte.
              > >
              > > Consider doing what ArrayList & MemoryStream do, over allocate the[/color][/color]
              buffer.[color=blue][color=green]
              > >
              > > Instead of having the buffer contain the exact number of bytes in it,[/color][/color]
              have[color=blue][color=green]
              > > it contain Capicity # of bytes, then have a second variable for the[/color][/color]
              Length[color=blue][color=green]
              > > number of bytes.
              > >
              > > I posted the start of a ByteBuffer class that functions very similar to[/color][/color]
              the[color=blue][color=green]
              > > System.Text.Str ingBuffer class in that it maintains a byte array[/color][/color]
              internally[color=blue][color=green]
              > > allowing you to append byte arrays on the end, expanding the internal[/color][/color]
              array[color=blue][color=green]
              > > as needed.
              > >
              > >[/color][/color]
              http://groups.google.com/groups?q=By...phx.gbl&rnum=3[color=blue][color=green]
              > >
              > > Hope this helps
              > > Jay
              > >
              > > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
              > > news:FAD79C2A-122C-4F0A-B193-8BB45F2E8941@mi crosoft.com...[color=darkred]
              > > > I was trying to determine the fastest way to build a byte array from
              > > > components where the size of the individual components varied[/color][/color][/color]
              depending on[color=blue][color=green][color=darkred]
              > > > the user's input. I tried three classes I built: (1) using redim[/color][/color][/color]
              arrays[color=blue][color=green]
              > > to[color=darkred]
              > > > add to a normal byte array (2) using an ArrayList and finally (3)[/color][/color][/color]
              using a[color=blue][color=green][color=darkred]
              > > > memorystream. These three classes are listed below the test sub[/color][/color][/color]
              called[color=blue][color=green][color=darkred]
              > > > "TestBuildByteA rray". It was interesting that using the memorystream[/color][/color][/color]
              was[color=blue][color=green][color=darkred]
              > > > twice as fast as the ArrayList and almost 2000 time as fast as using[/color]
              > > normal[color=darkred]
              > > > byte array with redimensions. Thought some might be interested in[/color][/color][/color]
              this.[color=blue][color=green][color=darkred]
              > > >
              > > > Private Sub TestBuildByteAr raySpeed(ByRef timeByteArray As Long,[/color][/color][/color]
              ByRef[color=blue][color=green][color=darkred]
              > > > 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,[/color][/color][/color]
              10,[color=blue][color=green]
              > > 11,[color=darkred]
              > > > 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.Ti cks
              > > > Dim cb As ByteArray = New ByteArray(start size)
              > > > For i = 1 To count
              > > > For j = 0 To 50 : cb.Add(byt) : Next
              > > > cb.AddRange(nb)
              > > > Next
              > > > b = cb.ToArray
              > > > timeByteArray = DateTime.Now.Ti cks - starttime
              > > >
              > > > 'Test ArrayList Class
              > > > ReDim b(0)
              > > > starttime = DateTime.Now.Ti cks
              > > > Dim cl As ArrayList = New ArrayList(start size)
              > > > For i = 1 To count
              > > > For j = 0 To 50 : cl.Add(byt) : Next
              > > > cl.AddRange(nb)
              > > > Next
              > > > b = DirectCast(cl.T oArray(GetType( Byte)), Byte())
              > > > timeArrayList = DateTime.Now.Ti cks - starttime
              > > >
              > > > 'Test ArrayStream Class
              > > > ReDim b(0)
              > > > starttime = DateTime.Now.Ti cks
              > > > 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.Ti cks - 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(si ze)
              > > > 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
              > > >
              > > > --
              > > > Dennis in Houston[/color]
              > >
              > >
              > >[/color][/color]


              Comment

              Working...