如何在新的.NET Framework 4.6中启用SIMD? [英] How to enable SIMD in a new .NET Framework 4.6?

查看:118
本文介绍了如何在新的.NET Framework 4.6中启用SIMD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个代码进行测试,哪种方法更快。我在x64版中运行代码。我有使用.NET Framework 4.6的VS 2015 Pro。

I wrote a code to test, which way is faster. I run the code in Release x64. I have VS 2015 Pro with .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,需要212ms的时间进行计算。如果我将其切换到我的库(这只是计算它的简单C#代码),则也需要大约210毫秒。那有点奇怪吧?我的SIMD应该更快!

Well, if i run it using System.Numerics from Framework 4.6 ( Nuget version ), it takes 212ms to calculate. If i switch it to my library, which is just simple c# code to calculate it, it also takes around 210 ms. That's a bit strange right ? I though SIMD should be must faster !

顺便说一句,IsHardwareAccelerated返回 True。

By the way, IsHardwareAccelerated returns "True".

那又怎样?我做错了????

So what I am doing wrong ???

仅供参考:没有SIMD的C ++运行390ms和77ms的SIMD。

推荐答案

我反编译了System.Numerics.Vector,我意识到,SIMD仅针对Vector而不是Matrix4x4实现。因此,汉斯·帕桑(Hans Passant)是对的。

I decompiled System.Numerics.Vector and I realized, SIMD is implemented only for Vectors and not for Matrix4x4. So Hans Passant was right.

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

I hope they will also add support for SIMD Matrix soon.

这篇关于如何在新的.NET Framework 4.6中启用SIMD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆