I wrote a blog “Faster List.ToArray() and Copying in C#” a while back, which showed several ways to copy from List to Array faster. One of these ways was implemented in the HPCsharp nuget package and increased performance by 4X using multiple processor cores. In this blog I’ll show how to do the same for Array.ToArray() and Array.CopyTo() functions for copying Array to another Array.
Exactly the Same
Just like the functions to copy a List to Array, C# provides the same functions for copying Array to another Array:
int[] arraySource = new int[] { 5, 7, 16, 3 }; int[] arrayDestination = arraySource.ToArray();
In the above case, ToArray creates a new Array and copies contents to it.
C# also provides CopyTo for Array to Array copy to a pre-existing Array.
Performance is the Same
One Core Is Not Enough
https://stackoverflow.com/questions/56803987/memory-bandwidth-for-many-channels-x86-systems
The above link shows that on Xeon and desktop processors a single thread is not sufficient to use all of the system memory bandwidth. On dual-memory
channel desktop systems, two threads are necessary to saturate system memory. On Xeon workstation and cloud systems, many-many threads and cores are needed.
HPCsharp Parallel Copy Functions
HPCsharp nuget package, on nuget.org, provides several high performance multi-core copy functions that speed up both of the cases above:
- destination Array is brand new: 2.5X to 3X speedup
- destination Array is being re-used: about 15% speedup
HPCsharp copy functions provide the familiar interfaces of List.ToArray() and List.CopyTo(). For Array, the same functions are also provided. When an new array is being returned by List.ToArray() or Array.CopyTo() functions, that array is brand new and has not been allocated yet. However, because multiple cores are being used, these HPCsharp functions nearly 3X faster on a quad-core laptop with two memory channels.
For the second case, when the destination Array has already been paged-in (i.e being re-used), HPCsharp copy functions provide a slight speedup of 15%, as the performance on my laptop is already near the available system memory bandwidth.
Possibly, on systems with more memory channels, such as Intel Xeon or AMD EPYC workstation/cloud processors, these functions may provide much higher speedup.
HPCsharp Examples
Each function has a self documenting interface that explains all of the arguments and discusses its unique attributes. This is a good place to start.
I’ve added examples of copy functions usage to the Examples VisualStudio 2019 solution included with HPCSharp open source repository, to demonstrate usage and to show that the interfaces are the same as C# Linq.