public static void IncrementByRef(ref int x)
{
x++;
}
public unsafe static void IncrementByPointer(int* x)
{
(*x)++;
}
int i = 30;
IncrementByRef(ref i);
// i = 31
unsafe{
IncrementByPointer(&i);
}
// i = 32
int i = 42; ref var x = ref i; x = x + 1; // i = 43
public static ref int GetArrayRef(int[] items, int index) => ref items[index];
<ItemGroup> <PackageReference Include="System.Memory" Version="4.4.0-preview1-25305-02" /> <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0-preview1-25305-02" /> </ItemGroup>
public struct Span<T> {
ref T _reference;
int _length;
public ref T this[int index] { get {...} }
...
}
public struct ReadOnlySpan<T> {
ref T _reference;
int _length;
public T this[int index] { get {...} }
...
}
string content = "content-length:123";
Stopwatch watch1 = new Stopwatch();
watch1.Start();
for (int j = 0; j < 100000; j++)
{
int.Parse(content.Substring(15));
}
watch1.Stop();
Console.WriteLine("\tTime Elapsed:\t" + watch1.ElapsedMilliseconds.ToString("N0") + "ms");
string content = "content-length:123";
ReadOnlySpan<char> span = content.ToCharArray();
span.Slice(15).ParseToInt();
watch.Start();
for (int j = 0; j < 100000; j++)
{
int icb = span.Slice(15).ParseToInt();
}
watch.Stop();
Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
public static class ReadonlySpanxtension
{
public static int ParseToInt(this ReadOnlySpan<char> rspan)
{
Int16 sign = 1;
int num = 0;
UInt16 index = 0;
if (rspan[0].Equals('-')){
sign = -1; index = 1;
}
for (int idx = index; idx < rspan.Length; idx++){
char c = rspan[idx];
num = (c - '0') + num * 10;
}
return num * sign;
}
}
String Substring Convert: Time Elapsed: 18ms ReadOnlySpan Convert: Time Elapsed: 4ms
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有