Faster way to fill a byte array

  • Thread starter Thread starter D. Yates
  • Start date Start date
D

D. Yates

All,

Is there a faster way to do this in C#:

byte[] buffer;
buffer = new byte[43];

for(int i = 0; i < buffer.Length; i++)
buffer = 0;


Dave
 
D. Yates said:
All,

Is there a faster way to do this in C#:

byte[] buffer;
buffer = new byte[43];

for(int i = 0; i < buffer.Length; i++)
buffer = 0;


Yes:

byte[] buffer = new byte[43];

It's guaranteed to be all zeroes to start with.
 
Dave,

Yes:

byte[] buffer;
buffer = new byte[43];

By default, elements of arrays of structures are the same as those
structures with the bits zeroed out, which is exactly what you want.

Hope this helps.
 
Hello,

If you just want a 0 initialized array, you don't have to use any for loop
as the .net framework already make sure that every single value in your
array is 0.

if you want to initialize to some other values than 0, you can do the
following :

byte[] aoe = new byte[] {4, 5, 3, 0, 0, 0, 2, 2};
 
Jon,

Assuming the byte array is being reused often, I was looking for a faster
way of doing this:

for(int i = 0; i < buffer.Length; i++)
buffer = 0;

Dave

Jon Skeet said:
D. Yates said:
All,

Is there a faster way to do this in C#:

byte[] buffer;
buffer = new byte[43];

for(int i = 0; i < buffer.Length; i++)
buffer = 0;


Yes:

byte[] buffer = new byte[43];

It's guaranteed to be all zeroes to start with.
 
byte[] buffer;
// array is used, values set, etc.
Array.Clear(buffer, 0, buffer.Length);
// array is now set to all zeros (or default values of whatever type
the array is)
 
Dave,

You might get faster performance by passing the array to the static
Initialize method on the Array class.

However, the question has to be asked, why not just create a new array
when needed? If you are worried about memory concerns, that's what the GC
is for.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

D. Yates said:
Jon,

Assuming the byte array is being reused often, I was looking for a faster
way of doing this:

for(int i = 0; i < buffer.Length; i++)
buffer = 0;

Dave

Jon Skeet said:
D. Yates said:
All,

Is there a faster way to do this in C#:

byte[] buffer;
buffer = new byte[43];

for(int i = 0; i < buffer.Length; i++)
buffer = 0;


Yes:

byte[] buffer = new byte[43];

It's guaranteed to be all zeroes to start with.
 
Brant,

Thanks, that's what I was looking for. The funny thing is that the Dev
Partner profiler states that my brute force method of clearing an array of
18 bytes was faster than calling the Array.Clear method.

Thanks again,
Dave
 
Nicholas,
However, the question has to be asked, why not just create a new array
when needed? If you are worried about memory concerns, that's what the GC
is for.

Well, I'm trying to make a method that is called a lot more efficient. I
came up with three version:

Version 1:
public static void StringToFixedSizeByteArray(string sValue, byte[]
buffer)
{
if (buffer != null)
{
if ((sValue != null) && (sValue.Length > 0))
{
for(int i = 0; i < buffer.Length; i++)
{
if (i < sValue.Length)
buffer = (byte) sValue;
else buffer = 0;
}
}
else
{
Array.Clear(buffer, 0, buffer.Length);
// for(int i = 0; i < buffer.Length; i++)
// buffer = 0;
}
}
}

and finally:

Version 2:
public static byte[] StringToFixedSizeByteArray(string sValue, int
iFixedSize)
{
byte[] buffer;

if (iFixedSize > 0)
{
buffer = new byte[iFixedSize];
if ((sValue != null) && (sValue.Length > 0))
{
if (sValue.Length < iFixedSize)
System.Text.Encoding.UTF8.GetBytes(
sValue, 0, sValue.Length, buffer, 0);
else System.Text.Encoding.UTF8.GetBytes(
sValue, 0, iFixedSize, buffer, 0);
}
}
else buffer = null;

return buffer;
}


Version 3:
public static byte[] StringToFixedSizeByteArray(string sValue, int
iFixedSize)
{
byte[] buffer;

if (iFixedSize > 0)
{
buffer = new byte[iFixedSize];
if ((sValue != null) && (sValue.Length > 0))
{
for(int i = 0; i < buffer.Length; i++)
{
if (i < sValue.Length)
buffer = (byte) sValue;
else break;
}
}
}
else buffer = null;

return buffer;
}


Version 3 is the fastest; however, I think I'm going to go with Version 2.


Dave
 
Actually, now that I think of it, just simply reinitializing the array
does the same thing, and a little more than twice as performant:

byte[] buffer = new byte[] {1,2,3,4,5,6,7,8,9};
buffer = new byte[buffer.Length];
 
Brant Estes said:
Actually, now that I think of it, just simply reinitializing the array
does the same thing, and a little more than twice as performant:

byte[] buffer = new byte[] {1,2,3,4,5,6,7,8,9};
buffer = new byte[buffer.Length];

Is it twice as performant when you include garbage collection though?
In particular, if each of those byte arrays lives until generation 1,
that could cost you quite a lot overall.
 
Back
Top