我正在嘗試使用System.Numerics.Vector<T>
(documentation)。爲什麼Vector。計數是靜態的?
我寫了一個簡單的單元測試:
var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 });
Assert.AreEqual(3, v.Count);
但它給了我一個生成錯誤:
Member 'Vector.Count' cannot be accessed with an instance reference; qualify it with a type name instead
出乎我的意料,Vector<T>.Count
是靜態的。
所以,我想:
var v = new System.Numerics.Vector<double>(new double[] { 12, 13, 14 });
Assert.AreEqual(3, Vector<double>.Count);
現在的代碼生成,但單元測試失敗:
Assert.AreEqual failed. Expected:<3>. Actual:<2>.
這是怎麼回事?
調查,我發現:
Assert.AreEqual(2, Vector<double>.Count);
Assert.AreEqual(4, Vector<float>.Count);
Assert.AreEqual(4, Vector<int>.Count);
Assert.AreEqual(2, Vector<long>.Count);
您的兩個片段是相同的。 – BoltClock
@BoltClock謝謝修復。 –
那麼,你正在發現爲什麼這個類沒有被添加到框架中。在Haswell或Broadwell處理器上,您獲得的價值也很可能是錯誤的,因爲它將使用AVX2提供的256位YMM寄存器,所以它應該是該值的兩倍。在桌子上留下2個perf,難以隱藏實現細節。 AVX-512即將推出:) –