Any Faster Way of Copying Arrays in C#

Any faster way of copying arrays in C#?

Use Buffer.BlockCopy. Its entire purpose is to perform fast (see Buffer):

This class provides better performance for manipulating primitive types than similar methods in the System.Array class.

Admittedly, I haven't done any benchmarks, but that's the documentation. It also works on multidimensional arrays; just make sure that you're always specifying how many bytes to copy, not how many elements, and also that you're working on a primitive array.

Also, I have not tested this, but you might be able to squeeze a bit more performance out of the system if you bind a delegate to System.Buffer.memcpyimpl and call that directly. The signature is:

internal static unsafe void memcpyimpl(byte* src, byte* dest, int len)

It does require pointers, but I believe it's optimized for the highest speed possible, and so I don't think there's any way to get faster than that, even if you had assembly at hand.


Update:

Due to requests (and to satisfy my curiosity), I tested this:

using System;
using System.Diagnostics;
using System.Reflection;

unsafe delegate void MemCpyImpl(byte* src, byte* dest, int len);

static class Temp
{
//There really should be a generic CreateDelegate<T>() method... -___-
static MemCpyImpl memcpyimpl = (MemCpyImpl)Delegate.CreateDelegate(
typeof(MemCpyImpl), typeof(Buffer).GetMethod("memcpyimpl",
BindingFlags.Static | BindingFlags.NonPublic));
const int COUNT = 32, SIZE = 32 << 20;

//Use different buffers to help avoid CPU cache effects
static byte[]
aSource = new byte[SIZE], aTarget = new byte[SIZE],
bSource = new byte[SIZE], bTarget = new byte[SIZE],
cSource = new byte[SIZE], cTarget = new byte[SIZE];

static unsafe void TestUnsafe()
{
Stopwatch sw = Stopwatch.StartNew();
fixed (byte* pSrc = aSource)
fixed (byte* pDest = aTarget)
for (int i = 0; i < COUNT; i++)
memcpyimpl(pSrc, pDest, SIZE);
sw.Stop();
Console.WriteLine("Buffer.memcpyimpl: {0:N0} ticks", sw.ElapsedTicks);
}

static void TestBlockCopy()
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
for (int i = 0; i < COUNT; i++)
Buffer.BlockCopy(bSource, 0, bTarget, 0, SIZE);
sw.Stop();
Console.WriteLine("Buffer.BlockCopy: {0:N0} ticks",
sw.ElapsedTicks);
}

static void TestArrayCopy()
{
Stopwatch sw = Stopwatch.StartNew();
sw.Start();
for (int i = 0; i < COUNT; i++)
Array.Copy(cSource, 0, cTarget, 0, SIZE);
sw.Stop();
Console.WriteLine("Array.Copy: {0:N0} ticks", sw.ElapsedTicks);
}

static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
TestArrayCopy();
TestBlockCopy();
TestUnsafe();
Console.WriteLine();
}
}
}

The results:

Buffer.BlockCopy: 469,151 ticks
Array.Copy: 469,972 ticks
Buffer.memcpyimpl: 496,541 ticks

Buffer.BlockCopy: 421,011 ticks
Array.Copy: 430,694 ticks
Buffer.memcpyimpl: 410,933 ticks

Buffer.BlockCopy: 425,112 ticks
Array.Copy: 420,839 ticks
Buffer.memcpyimpl: 411,520 ticks

Buffer.BlockCopy: 424,329 ticks
Array.Copy: 420,288 ticks
Buffer.memcpyimpl: 405,598 ticks

Buffer.BlockCopy: 422,410 ticks
Array.Copy: 427,826 ticks
Buffer.memcpyimpl: 414,394 ticks

Now change the order:

Array.Copy: 419,750 ticks
Buffer.memcpyimpl: 408,919 ticks
Buffer.BlockCopy: 419,774 ticks

Array.Copy: 430,529 ticks
Buffer.memcpyimpl: 412,148 ticks
Buffer.BlockCopy: 424,900 ticks

Array.Copy: 424,706 ticks
Buffer.memcpyimpl: 427,861 ticks
Buffer.BlockCopy: 421,929 ticks

Array.Copy: 420,556 ticks
Buffer.memcpyimpl: 421,541 ticks
Buffer.BlockCopy: 436,430 ticks

Array.Copy: 435,297 ticks
Buffer.memcpyimpl: 432,505 ticks
Buffer.BlockCopy: 441,493 ticks

Now change the order again:

Buffer.memcpyimpl: 430,874 ticks
Buffer.BlockCopy: 429,730 ticks
Array.Copy: 432,746 ticks

Buffer.memcpyimpl: 415,943 ticks
Buffer.BlockCopy: 423,809 ticks
Array.Copy: 428,703 ticks

Buffer.memcpyimpl: 421,270 ticks
Buffer.BlockCopy: 428,262 ticks
Array.Copy: 434,940 ticks

Buffer.memcpyimpl: 423,506 ticks
Buffer.BlockCopy: 427,220 ticks
Array.Copy: 431,606 ticks

Buffer.memcpyimpl: 422,900 ticks
Buffer.BlockCopy: 439,280 ticks
Array.Copy: 432,649 ticks

or, in other words: they're very competitive; as a general rule, memcpyimpl is fastest, but it's not necessarily worth worrying about.

Fast array copy in C#

I would checkout the System.Buffer.BlockCopy if you are really concerned about speed.

http://msdn.microsoft.com/en-us/library/system.buffer.blockcopy.aspx

Simple Example:

  int[] a = new int[] {1,2,3,4,5,6,7,8};
int[] b = new int[a.Length];
int size = sizeof(int);
int length = a.Length * size;
System.Buffer.BlockCopy(a, 0, b, 0, length);

Great discussion on it over here: Array.Copy vs Buffer.BlockCopy

Fastest method for copying a string array in C# - direct assigning or using Copy(), CopyTo() or Clone()

Array.CopyTo is just a wrapper around Array.Copy. That is, CopyTo does essentially this:

void CopyTo(Array dest, int length)
{
Array.Copy(this, dest, length);
}

So Copy will be slightly faster than CopyTo (one less indirection).

Your direct copy doesn't actually copy the array. It just copies the reference. That is, given this code:

    string[] directAnimals = new string[animals.Length];
directAnimals = animals;

If you then write animals[0] = "Penguin";, then directAnimals[0] will also contain the value "Penguin".

I suspect that Clone will be the same as Array.Copy. All it does it allocate a new array and copy the values to it.

Some notes on timing:

Your tests are doing far too little work to time accurately. If you want meaningful results, you'll have to execute each test many times. Something like:

copyTo.Start();
for (int i = 0; i < 1000; ++i)
{
string[] copyToAnimals = new string[animals.Length];
animals.CopyTo(copyToAnimals, 0);
}
copyTo.Stop();
Console.WriteLine("Copy to: " + copyTo.Elapsed);

For such small arrays, 1,000 times might not even be enough. You might need a million just to see if there's any meaningful difference.

Also, your results will be meaningless if you run these tests in the debugger. Be sure to compile in release mode and run with the debugger detached. Either execute from the command line, or use Ctrl+F5 in Visual Studio.

What is the fastest way to copy my array?

There is also:

Array.Copy
Array.CopyTo

but whether these will be faster will require profiling.

But be warned about focusing on micro-optimisations to the extent you miss the big picture, on modern PCs the effect of multi-level memory caching is likely to be greater than one approach or another to the copy.

Edit: Quick check in reflector: both of the above methods boil down to a common native implementation (good).

Note the docs for Array.Copy cover valid type conversions, a value -> value widening conversion like byte to float should be OK.

Copy Arrays to Array

I try to copy a Int Array into 2 other Int Arrays with

The first thing that is important is that in this line :

unsortedArray2 = unsortedArray;

you do not copy the values of the unsortedArray into unsortedArray2. The = is called the assignment operator

The assignment operator (=) stores the value of its right-hand operand in the storage location,

Now the second thing that you need to know to understand this phenomenon is that there are 2 types of objects in C# Reference Types and Value types

The documentation explains it actually quite nicely:

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

The solution can be to use the Array.Copy method.

Array.Copy(unsortedArray, 0, unsortedArray2 , 0, unsortedArray.Length);

The CopyTo method would also work in this case

unsortedArray.CopyTo(unsortedArray2 , 0);

Note:this will work because the content of the array is a value type! If it would be also of reference type, changing a sub value of one of the items would lead also to a change in the same item in the destination array.

Fast way to copy part of an array into a List?

The List<T>(IEnumerable<T>) constructor will use ICollection<T>.CopyTo if the collection implements ICollection<T>, which byte[] will do.

That's not going to help directly if you only want to extract part of the array, but you could create your own ByteArraySegment class implementing ICollection<byte> and implement the CopyTo operation using Buffer.BlockCopy or whatever:

public class ByteArraySegment : ICollection<byte>
{
private readonly byte[] array;
private readonly int start;
private readonly int count;

public ByteArraySegment(...)
{
// Obvious code
}

public void CopyTo(byte[] target, int index)
{
Buffer.BlockCopy(array, start, target, index, count);
}

// Other ICollection<T> members
}

Then:

List<byte> bytes = new List<byte>(new ByteArraySegment(myArray, start, count));

(Or use AddRange which has the same optimization.)

Copy array to struct array as fast as possible in C#

Yes, Marshal.Copy is the way to go. I've answered a similar question here.

Here's a generic method to copy from struct[] to byte[] and vice versa

private static byte[] ToByteArray<T>(T[] source) where T : struct
{
GCHandle handle = GCHandle.Alloc(source, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
byte[] destination = new byte[source.Length * Marshal.SizeOf(typeof(T))];
Marshal.Copy(pointer, destination, 0, destination.Length);
return destination;
}
finally
{
if (handle.IsAllocated)
handle.Free();
}
}

private static T[] FromByteArray<T>(byte[] source) where T : struct
{
T[] destination = new T[source.Length / Marshal.SizeOf(typeof(T))];
GCHandle handle = GCHandle.Alloc(destination, GCHandleType.Pinned);
try
{
IntPtr pointer = handle.AddrOfPinnedObject();
Marshal.Copy(source, 0, pointer, source.Length);
return destination;
}
finally
{
if (handle.IsAllocated)
handle.Free();
}
}

Use it as:

[StructLayout(LayoutKind.Sequential)]
public struct Demo
{
public double X;
public double Y;
}

private static void Main()
{
Demo[] array = new Demo[2];
array[0] = new Demo { X = 5.6, Y = 6.6 };
array[1] = new Demo { X = 7.6, Y = 8.6 };

byte[] bytes = ToByteArray(array);
Demo[] array2 = FromByteArray<Demo>(bytes);
}


Related Topics



Leave a reply



Submit