473,225 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 98369
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
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
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
11
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
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
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
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
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
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
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.