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…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索