Requirements: Recently, looking at knowledge<T> such as Span and Memory<T>, I feel that there are many advantages to using such objects, such as: direct access to memory (and very secure), reduction of unnecessary memory allocation (depending on the scene, e.g., Substring interception of string conversion), modification of values in memory, and still learning, not that in-depth understanding.
System.Span<T> is a new value type in the .NET core. It supports a representation of a contiguous region of arbitrary memory, whether that memory is associated with a managed object, provided by native code through interop, or is in the stack. It does so while still providing secure access, with performance characteristics similar to arrays. In use, Span ensures memory and data security with little to no overhead.
For example, if we use Substring to convert a string to type int, guid, long, etc., we will no longer use the intercepted string content, but calling Substring to return a new string will request new memory space.
First, the BenchmarkDotNet benchmarking tool is introduced, as follows:
The test code is as follows:
The results are as follows:
Reference:
The hyperlink login is visible.
The hyperlink login is visible.
|