This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3616|Reply: 2

[Source] .NET/C# uses Span to handle strings with high performance

[Copy link]
Posted on 5/25/2023 8:13:18 PM | | | |
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:

.NET/C# uses BenchmarkDotNet to test code performance
https://www.itsvse.com/thread-9576-1-1.html
The test code is as follows:

The results are as follows:



Reference:

The hyperlink login is visible.
The hyperlink login is visible.





Previous:.NET/C# syntax goto statement
Next:Docker builds online mind maps based on draw.io
Posted on 5/26/2023 2:11:02 PM |
Come and learn...
 Landlord| Posted on 9/1/2023 9:59:03 PM |
If yes.NET frameworkcan be referencedSystem.Memory.dllUse ReadOnlySpan, AsSpan
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com