I am currently doing it in a for loop, and I know in C there is the ZeroMemory API, however that doesn't seem to be available in C#. Nor does the somewhat equivalent Array.fill from Java exist either. I am just wondering if there is an easier/faster way?
Try Array.Clear():
Sets a range of elements in the Array to zero, to
false
, or tonull
(Nothing in Visual Basic), depending on the element type.
C++: memset(array,0,sizeof(array))
C#: Array.Clear (Array array,int index, int length)
Java: Arrays.fill(array, val)
Several people have posted answers, then deleted them, saying that in any language a for loop will be equally performant as a memset or FillMemory or whatever.
For example, a compiler might chunk it into 64-bit aligned pieces to take advantage of a 64bit zero assignment instruction, if available. It will take alignment and stuff into consideration. Memset's implementation is certainly not trivial.
one memset.asm. Also see memset-is-faster-than-simple-loop.html.
Never underestimate the infinite deviousness of compiler and standard library writers.