gpt4 book ai didi

c# - 如何在新的 .NET Framework 4.6 中启用 SIMD?

转载 作者:太空狗 更新时间:2023-10-29 23:38:01 24 4
gpt4 key购买 nike

我写了个代码测试一下,哪种方式更快。我在 Release x64 中运行代码。我有 VS 2015 Pro 和 .NET Framework 4.6。

#if SYSTEMMATHLIB
using System.Numerics;
#else
using Engine.Mathematic;
#endif

namespace Engine.Editor
{
public static class Program
{
public static void Main()
{
#if SYSTEMMATHLIB
Console.WriteLine("Hardware Acceleration: " + Vector.IsHardwareAccelerated.ToString());
#endif

Stopwatch sw = new Stopwatch();
sw.Start();

#if SYSTEMMATHLIB
for (int i = 0; i < 1000000; i++)
{
Matrix4x4 m = Matrix4x4.CreateRotationZ(0.50f);
m.Translation = new Vector3(5, 10, 15);

Matrix4x4 m2 = Matrix4x4.CreateRotationX(0.50f);
m2.Translation = new Vector3(-5, 10, -15);

Matrix4x4 m3 = m * m2;
Matrix4x4 inv; Matrix4x4.Invert(m3, out inv);
}
#else
for (int i = 0; i < 1000000; i++)
{
Matrix m = Matrix.RotationZ(0.50f);
m.TranslationVector = new Vector3(5, 10, 15);

Matrix m2 = Matrix.RotationX(0.50f);
m2.TranslationVector = new Vector3(-5, 10, -15);

Matrix m3 = m * m2;
Matrix inv; Matrix.Invert(ref m3, out inv);
}
#endif
long mili = sw.ElapsedMilliseconds;
sw.Stop();

Console.WriteLine("Total mili: " + mili.ToString());
Console.ReadLine();
}
}
}

好吧,如果我使用 Framework 4.6(Nuget 版本)中的 System.Numerics 运行它,计算需要 212 毫秒。如果我将它切换到我的库,这只是简单的 c# 代码来计算它,它也需要大约 210 毫秒。这有点奇怪吧?我虽然 SIMD 应该必须更快!

顺便说一句,IsHardwareAccelerated 返回“True”。

所以我做错了什么???

仅供引用:不带 SIMD 的 C++ 运行 390 毫秒,带 SIMD 运行 77 毫秒。

最佳答案

我反编译了 System.Numerics.Vector 并意识到,SIMD 仅针对 Vectors 而不是 Matrix4x4 实现。所以 Hans Passant 是对的。

我希望他们也能尽快添加对 SIMD Matrix 的支持。

关于c# - 如何在新的 .NET Framework 4.6 中启用 SIMD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31906215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com