473,508 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 98713
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
26886
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
19
4108
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
11
30548
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
15
34565
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
2
4634
by: Ole | last post by:
Hi, Is there a better / faster way to convert a byte array to a short array than to use Convert.toInt16(bytearray, i) in a loop? Thanks Ole
20
3462
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
23
16342
by: Gerrit | last post by:
Hi all, I'm getting an OutOfMemoryException when I initialize a byte array in C# like this: Byte test = new Byte; I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing...
1
3377
by: trevor.farchild | last post by:
Hi, long time reader, first time poster I have an application that will be doing this 5 times a second: Get a bunch of data from a NetworkStream and convert it into a Bitmap Therefore, the...
2
2012
by: active | last post by:
I have another post asking how to fill in the structure but I now think GetDIBBits is the way, I just can't get it to work! I can make the following run but GetDIBBits does not fill the array to...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.