Write high-performance C# code – Span
Write high-performance C# code – Span Span provides a type-safe and memory-safe representation of a contiguous region of arbitrary memory. It is a ref structure allocated on the stack rather than the managed heap, and is an abstraction of an arbitrary block of memory. 1. About Span First introduced in NET Core 2.1 Provides reading and writing of any contiguous area of memoryView Use indexing/iteration to modify memory in a range Almost no overhead 2. Relationship with memory Span representsa contiguous area of any memory. Span instances are usually used to save elements of arrays or parts of arrays. However, unlike arrays, Span instances can point to managed memory on the stack, native memory, or managed memory. 3. Performance test Span is usually used to process arrays, so the scenario for this test Is the splitting of an array: Starting from the middle element of an array, obtain and return one quarter of the elements. 3.1 Array initialization Here we prepare an array to be initialized with three different lengths, which will help cover more test situations. 3.2 Three methods are used for testing, Compare BenchmarkDotNet: The first type (regular): The second type (copy): The third type (Span slice): The…