T
Trecius
Hello, Newsgroupians:
I've an optimization question for you all really quick. I have a stream
that I am reading some bytes. At times, the stream can contain a small
amount of bytes such as 50 or so or it can contain as much 10000000 bytes.
In reality, I do not know the maximum number of bytes.
In my function, I am going to read() the byte stream using a buffer. Now,
is it better to read it into a buffer and dump the buffer into a List<byte>
maybe using AddRange() or should I Array.Resize the buffer to grow a specific
size everytime?
Code for List<byte>
List<byte> lstBytes = new List<byte>();
byte[] buffer = new byte[2048];
while (stream.Read(buffer, 0, buffer.Length) != -1)
{
lstBytes.AddRange(buffer);
}
return lstBytes.ToArray();
Code for resizing array:
byte[] buffer = new byte[2048];
while (stream.Read(buffer, buffer.Length - 2048, 2048) != -1)
{
Array.Resize(ref buffer, buffer.Size + 2048);
}
return buffer;
So which way should I use? Should I dump it into a list everytime, or
should I resize the array everytime? Is there another way you would
recommend? Thank you all for your help and suggestions.
Trecius
I've an optimization question for you all really quick. I have a stream
that I am reading some bytes. At times, the stream can contain a small
amount of bytes such as 50 or so or it can contain as much 10000000 bytes.
In reality, I do not know the maximum number of bytes.
In my function, I am going to read() the byte stream using a buffer. Now,
is it better to read it into a buffer and dump the buffer into a List<byte>
maybe using AddRange() or should I Array.Resize the buffer to grow a specific
size everytime?
Code for List<byte>
List<byte> lstBytes = new List<byte>();
byte[] buffer = new byte[2048];
while (stream.Read(buffer, 0, buffer.Length) != -1)
{
lstBytes.AddRange(buffer);
}
return lstBytes.ToArray();
Code for resizing array:
byte[] buffer = new byte[2048];
while (stream.Read(buffer, buffer.Length - 2048, 2048) != -1)
{
Array.Resize(ref buffer, buffer.Size + 2048);
}
return buffer;
So which way should I use? Should I dump it into a list everytime, or
should I resize the array everytime? Is there another way you would
recommend? Thank you all for your help and suggestions.
Trecius