using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace System
{
[DefaultMember("Item")]
[IsByRefLike]
[IsReadOnly]
public struct Span<T>
{
public Span(T[] array);
[CLSCompliant(false)]
public Span(void* pointer, int length);
public Span(T[] array, int start, int length);
public ref T this[int index] { get; }
public static Span<T> Empty { get; }
public int Length { get; }
public bool IsEmpty { get; }
public void Clear();
public void CopyTo(Span<T> destination);
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Equals() on Span will always throw an exception. Use == instead.")]
public override bool Equals(object obj);
public void Fill(T value);
public Enumerator GetEnumerator();
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("GetHashCode() on Span will always throw an exception.")]
public override int GetHashCode();
[EditorBrowsable(EditorBrowsableState.Never)]
public ref T GetPinnableReference();
public Span<T> Slice(int start);
public Span<T> Slice(int start, int length);
public T[] ToArray();
public override string ToString();
public bool TryCopyTo(Span<T> destination);
public static bool operator ==(Span<T> left, Span<T> right);
public static bool operator !=(Span<T> left, Span<T> right);
public static implicit operator ReadOnlySpan<T>(Span<T> span);
public static implicit operator Span<T>(ArraySegment<T> segment);
public static implicit operator Span<T>(T[] array);
[IsByRefLike]
public struct Enumerator
{
public ref T Current { get; }
public bool MoveNext();
}
}
}
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Span<StructDog> list = new Span<StructDog>();
for (long i = 0; i < 100000; i++)
{
StructDog tank = new StructDog();
tank.myProperty = i;
tank.szSrcMach = "測試字符串" + i;
tank.address = "武松打虎";
tank.createTime = DateTime.Now;
tank.phone = "18454648646";
tank.updateTime = DateTime.Now;
tank.weight = 58.623;
tank.weight2 = 61.623;
tank.weight3 = 61.623;
tank.weight4 = 61.623;
tank.childrenList1 = new List<StructDog2>() {
new StructDog2(){
color="紅色呢",weight=18585L
},
new StructDog2(){
color="棕色呢",weight=845858L
}
};
tank.childrenList2 = new List<StructDog2>() {
new StructDog2(){
color="紅色呢",weight=18585L
},
new StructDog2(){
color="棕色呢",weight=845858L
}
};
list.Fill(tank);
}
foreach (var item in list)
{
long km = item.myProperty;
string kmg = item.szSrcMach;
string color = item.childrenList1[0].color;
//Console.WriteLine("Hello World!" + item.myProperty);
}
stopwatch.Stop();
Console.WriteLine("耗時" + stopwatch.ElapsedMilliseconds);
Console.ReadKey();
public struct StructDog
{
public long myProperty;
public string szSrcMach;
public double weight;
public double weight2;
public double weight3;
public double weight4;
public DateTime createTime;
public DateTime updateTime;
public string name;
public string phone;
public string address;
public List<StructDog2> childrenList1;
public List<StructDog2> childrenList2;
}
public struct StructDog2
{
public long weight;
public string color;
}
相同代碼用List實現,StructDog是class,不是結構struct ,會耗時多近一半時間,測試結果:
結論,Span高性能