469,270 Members | 1,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Faster way to fill a byte array

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[i] = 0;
Dave
Jan 11 '06 #1
10 94414
D. Yates <fo****@hotmail.com> wrote:
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[i] = 0;


Yes:

byte[] buffer = new byte[43];

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 11 '06 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:e$**************@TK2MSFTNGP14.phx.gbl...
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[i] = 0;
Dave

Jan 11 '06 #3
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};

--
Francois Beaussier

"D. Yates" <fo****@hotmail.com> a écrit dans le message de news:
e$**************@TK2MSFTNGP14.phx.gbl...
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[i] = 0;
Dave

Jan 11 '06 #4
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[i] = 0;

Dave

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
D. Yates <fo****@hotmail.com> wrote:
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[i] = 0;


Yes:

byte[] buffer = new byte[43];

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 11 '06 #5
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)

Jan 11 '06 #6
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]
- mv*@spam.guard.caspershouse.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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[i] = 0;

Dave

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
D. Yates <fo****@hotmail.com> wrote:
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[i] = 0;


Yes:

byte[] buffer = new byte[43];

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Jan 11 '06 #7
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
Jan 12 '06 #8
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];

Jan 12 '06 #9
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[i] = (byte) sValue[i];
else buffer[i] = 0;
}
}
else
{
Array.Clear(buffer, 0, buffer.Length);
// for(int i = 0; i < buffer.Length; i++)
// buffer[i] = 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[i] = (byte) sValue[i];
else break;
}
}
}
else buffer = null;

return buffer;
}
Version 3 is the fastest; however, I think I'm going to go with Version 2.
Dave
Jan 12 '06 #10
Brant Estes <br****@magenic.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Cook | last post: by
11 posts views Thread by Dan C | last post: by
15 posts views Thread by Kueishiong Tu | last post: by
20 posts views Thread by quantumred | last post: by
23 posts views Thread by Gerrit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.