Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append two or more byte arrays in C#

Tags:

c#

bytearray

Is there a best (see below) way to append two byte arrays in C#?

Pretending I have complete control, I can make the first byte array sufficiently large to hold the second byte array at the end and use the Array.CopyTo function. Or I can loop over individual bytes and make an assignment.

Are there better ways? I can't imagine doing something like converting the byte arrays to string and joining them and converting them back would be better than either method above.

In terms of best/better (in order):

  1. Fastest
  2. Least RAM consumption

A constraint is that I must work in the .NET 2.0 framework.

The two choices recommended are MemoryStream and BlockCopy. I have run a simple speed test of 10,000,000 loops 3 times and got the following results:

Average of 3 runs of 10,000,000 loops in milliseconds:

  • BlockCopy Time: 1154, with a range of 13 milliseconds
  • MemoryStream GetBuffer Time: 1470, with a range of 14 milliseconds
  • MemoryStream ToArray Time: 1895, with a range of 3 milliseconds
  • CopyTo Time: 2079, with a range of 19 milliseconds
  • Byte-by-byte Time: 2203, with a range of 10 milliseconds

Results of List<byte> AddRange over 10 million loops: List<byte> Time: 16694

Relative RAM Consumption (1 is baseline, higher is worse):

  • Byte-by-byte: 1
  • BlockCopy: 1
  • Copy To: 1
  • MemoryStream GetBuffer: 2.3
  • MemoryStream ToArray: 3.3
  • List<byte>: 4.2

The test shows that in general, unless you are doing a lot of byte copies [which I am], looking at byte copies is not worth a focus [e.g. 10 million runs yielding a difference of as much as 1.1 seconds].

like image 869
torial Avatar asked May 21 '09 20:05

torial


People also ask

How do you add two byte arrays?

The recommended solution to concatenate two or more byte arrays is using ByteArrayOutputStream . The idea is to write bytes from each of the byte arrays to the output stream, and then call toByteArray() to get the current contents of the output stream as a byte array.

How do I add a byte array?

If you have a byte array from elsewhere, you can use the AddRange function. mahByteArray. AddRange(mahOldByteArray); Then you can use Add() and Insert() to add elements.

How do you merge bytes?

byte[] one = getBytesForOne(); byte[] two = getBytesForTwo(); byte[] combined = new byte[one. length + two. length]; System. arraycopy(one,0,combined,0 ,one.


2 Answers

You want BlockCopy

According to this blog post it is faster than Array.CopyTo.

like image 52
dss539 Avatar answered Oct 22 '22 23:10

dss539


You could also use an approach with a MemoryStream. Suppose b1 and b2 are two byte arrays, you can get a new one, b3, by using the MemoryStream in the following fashion:

var s = new MemoryStream(); s.Write(b1, 0, b1.Length); s.Write(b2, 0, b2.Length); var b3 = s.ToArray(); 

This should work without LINQ and is in fact quite a bit faster.

like image 40
flq Avatar answered Oct 23 '22 00:10

flq