.net 为什么Mono在Linux和BSD上运行慢1/3?

.net 为什么Mono在Linux和BSD上运行慢1/3?,.net,linux,macos,mono,bsd,.net,Linux,Macos,Mono,Bsd,编辑:在下面的第二篇文章中查看更好的基准 我在Win8.1、Linux、BSD和OSX上对.NET4.5和Mono3.2.x进行了一些性能测试 请注意:这些测试是使用Mono x86或.NET x86 arch编译的。它们没有在虚拟机中运行。测试计算机采用三重引导,本机运行“Win8/Linux/BSD”,Mac采用“OSX/Win7”双引导。另请注意,“WIN32”编译器指令仅在Win8/Win7上用于“TimeBeginPeriod”,以强制Windows中的秒表精确。Linux/BSD/O

编辑:在下面的第二篇文章中查看更好的基准

我在Win8.1、Linux、BSD和OSX上对.NET4.5和Mono3.2.x进行了一些性能测试

请注意:这些测试是使用Mono x86或.NET x86 arch编译的。它们没有在虚拟机中运行。测试计算机采用三重引导,本机运行“Win8/Linux/BSD”,Mac采用“OSX/Win7”双引导。另请注意,“WIN32”编译器指令仅在Win8/Win7上用于“TimeBeginPeriod”,以强制Windows中的秒表精确。Linux/BSD/OSX不需要这个,但windows需要

这是测试代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

using numf = System.Single;
using numi = System.Int32;

#if WIN32
using System.Runtime.InteropServices;
#endif

namespace Benchmarks
{
    struct Vector4
    {
        public numf X, Y, Z, W;

        public static Vector4 operator+(Vector4 p1, Vector4 p2)
        {
            p1.X += p2.X;
            p1.Y += p2.Y;
            p1.Z += p2.Z;
            p1.W += p2.W;
            return p1;
        }

        public static Vector4 operator-(Vector4 p1, Vector4 p2)
        {
            p1.X -= p2.X;
            p1.Y -= p2.Y;
            p1.Z -= p2.Z;
            p1.W -= p2.W;
            return p1;
        }

        public static Vector4 operator*(Vector4 p1, Vector4 p2)
        {
            p1.X *= p2.X;
            p1.Y *= p2.Y;
            p1.Z *= p2.Z;
            p1.W *= p2.W;
            return p1;
        }

        public static Vector4 operator/(Vector4 p1, Vector4 p2)
        {
            p1.X /= p2.X;
            p1.Y /= p2.Y;
            p1.Z /= p2.Z;
            p1.W /= p2.W;
            return p1;
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}, {3}", X, Y, Z, W);
        }
    }

    class Program
    {
        #if WIN32
        [StructLayout(LayoutKind.Sequential)]
        public struct TimeCaps
        {
            public uint wPeriodMin;
            public uint wPeriodMax;
        }

        private static TimeCaps caps;

        [DllImport("winmm.dll", EntryPoint="timeGetDevCaps", SetLastError=true)]
        public static extern uint TimeGetDevCaps(ref TimeCaps timeCaps, uint sizeTimeCaps);

        [DllImport("winmm.dll", EntryPoint="timeBeginPeriod", SetLastError=true)]
        public static extern uint TimeBeginPeriod(uint uMilliseconds);

        [DllImport("winmm.dll", EntryPoint="timeEndPeriod", SetLastError=true)]
        public static extern uint TimeEndPeriod(uint uMilliseconds);

        public static void OptimizedMode()
        {
            caps = new TimeCaps();
            if (TimeGetDevCaps(ref caps, (uint)System.Runtime.InteropServices.Marshal.SizeOf(caps)) != 0)
            {
                Console.WriteLine("StopWatch: TimeGetDevCaps failed");
            }

            if (TimeBeginPeriod(caps.wPeriodMin) != 0)
            {
                Console.WriteLine("StopWatch: TimeBeginPeriod failed");
            }
        }

        public static void EndOptimizedMode()
        {
            if (TimeEndPeriod(caps.wPeriodMin) != 0)
            {
                Console.WriteLine("StopWatch: TimeEndPeriod failed");
            }
        }
        #endif

        static Random random;

        static void Main(string[] args)
        {
            #if WIN32
            OptimizedMode();
            #endif

            random = new Random();
            Console.WriteLine("Enter loop count:");
            Console.WriteLine("999999");
            string value = Console.ReadLine();
            int count;
            if (int.TryParse(value, out count))
            {
                runVector4Test(count);
            }
            else
            {
                Console.WriteLine("Invalide value: " + value);
            }

            #if WIN32
            EndOptimizedMode();
            #endif

            Console.WriteLine("DONE");
            Console.ReadLine();
        }

        static void runVector4Test(int count)
        {
            var values = new Vector4[count];
            const double range = .01;
            for (int i = 0; i != count; ++i)
            {
                values[i].X = (numf)(random.NextDouble() * range) + 1;
                values[i].Y = (numf)(random.NextDouble() * range) + 1;
                values[i].Z = (numf)(random.NextDouble() * range) + 1;
                values[i].W = (numf)(random.NextDouble() * range) + 1;
            }

            Console.WriteLine("Waiting for GC...");
            GC.Collect();
            System.Threading.Thread.Sleep(5000);
            Console.WriteLine("Starting Vector4 Test...");

            var time = new Stopwatch();
            long totalTime = 0;
            Vector4 totalValue = new Vector4();
            for (int i = 0; i != 100; ++i)
            {
                time.Restart();
                for (int i2 = 0; i2 < count-1; ++i2)
                {
                    Vector4 vec1 = values[i2];
                    Vector4 vec2 = values[i2+1];
                    totalValue += vec1;
                    totalValue -= vec2;
                    totalValue /= vec1;
                    totalValue *= vec2;
                }
                time.Stop();
                totalTime += time.ElapsedMilliseconds;
            }

            Console.WriteLine("Vector4 Time: " + (totalTime / 100d));
            Console.WriteLine("Vector4 Values: " + totalValue);
            Console.WriteLine();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
使用系统诊断;
使用numf=System.Single;
使用numi=System.Int32;
#如果是WIN32
使用System.Runtime.InteropServices;
#恩迪夫
命名空间基准
{
结构向量4
{
公共numf X,Y,Z,W;
公共静态向量4运算符+(向量4 p1,向量4 p2)
{
p1.X+=p2.X;
p1.Y+=p2.Y;
p1.Z+=p2.Z;
p1.W+=p2.W;
返回p1;
}
公共静态向量4运算符-(向量4 p1,向量4 p2)
{
p1.X-=p2.X;
p1.Y-=p2.Y;
p1.Z-=p2.Z;
p1.W-=p2.W;
返回p1;
}
公共静态向量4运算符*(向量4 p1,向量4 p2)
{
p1.X*=p2.X;
p1.Y*=p2.Y;
p1.Z*=p2.Z;
p1.W*=p2.W;
返回p1;
}
公共静态向量4运算符/(向量4 p1,向量4 p2)
{
p1.X/=p2.X;
p1.Y/=p2.Y;
p1.Z/=p2.Z;
p1.W/=p2.W;
返回p1;
}
公共重写字符串ToString()
{
返回string.Format(“{0},{1},{2},{3}”,X,Y,Z,W);
}
}
班级计划
{
#如果是WIN32
[StructLayout(LayoutKind.Sequential)]
公共结构时间上限
{
公共部门管理;
公共uint wPeriodMax;
}
私人静态时间上限;
[DllImport(“winmm.dll”,EntryPoint=“timeGetDevCaps”,SetLastError=true)]
公共静态外部uint TimeGetDevCaps(参考TimeCaps TimeCaps,uint sizeTimeCaps);
[DllImport(“winmm.dll”,EntryPoint=“timeBeginPeriod”,SetLastError=true)]
公共静态外部uint TimeBeginPeriod(uint UMillisSeconds);
[DllImport(“winmm.dll”,EntryPoint=“timeEndPeriod”,SetLastError=true)]
公共静态外部uint TimeEndPeriod(uint uMilliseconds);
公共静态void OptimizedMode()
{
上限=新的时间上限();
if(TimeGetDevCaps(ref caps,(uint)System.Runtime.InteropServices.Marshal.SizeOf(caps))!=0)
{
Console.WriteLine(“秒表:TimeGetDevCaps失败”);
}
if(TimeBeginPeriod(caps.wPeriodMin)!=0)
{
Console.WriteLine(“秒表:TimeBeginPeriod失败”);
}
}
公共静态void intopimizedMode()
{
if(TimeEndPeriod(caps.wPeriodMin)!=0)
{
Console.WriteLine(“秒表:TimeEndPeriod失败”);
}
}
#恩迪夫
静态随机;
静态void Main(字符串[]参数)
{
#如果是WIN32
优化模式();
#恩迪夫
随机=新随机();
WriteLine(“输入循环计数:”);
控制台写入线(“999999”);
字符串值=Console.ReadLine();
整数计数;
if(int.TryParse(值,超出计数))
{
运行向量器4测试(计数);
}
其他的
{
控制台写入线(“无效值:”+值);
}
#如果是WIN32
内时序模式();
#恩迪夫
控制台。写入线(“完成”);
Console.ReadLine();
}
静态无效runVector4Test(int计数)
{
var值=新向量4[计数];
常数双量程=.01;
for(int i=0;i!=count;++i)
{
值[i].X=(numf)(random.NextDouble()*range)+1;
值[i].Y=(numf)(random.NextDouble()*range)+1;
值[i].Z=(numf)(random.NextDouble()*range)+1;
值[i].W=(numf)(random.NextDouble()*range)+1;
}
Console.WriteLine(“等待GC…”);
GC.Collect();
系统线程线程睡眠(5000);
Console.WriteLine(“启动矢量4测试…”);
var time=新秒表();
长总时间=0;
Vector4 totalValue=新Vector4();
对于(int i=0;i!=100;++i)
{
time.Restart();
对于(int i2=0;i2
以下是结果

<<< AMD Athlon 64 X2 Dual Core 4600+ 2.40GHz >>>
{
.NET 4.5 (Win8-Win32) = 39.9 mil

Mono 3.2.3 (Win8-Win32) = 99.49 mil
Mono 3.2.3 (PC-Linux) = 146.87 mil
Mono 3.2.1 (PC-BSD) = 144 mil
}


<<< Intel Core2 Duo P8600 2.40GHz >>>
{
Mono 3.2.3 (OSX 10.9) = 98.54 mil
.NET 4.5 (Win7-Win32) = 38.47 mil
}
>
{
.NET 4.5(Win8-Win32)=39.9 mil
单声道3.2.3(Win8-Win32)=99.49密耳
Mono 3.2.3(PC Linux)=146.87 mil
单声道3.2.1(PC-BSD)=144密耳
}
>
{
单声道3.2.3(OSX 10.9)=98.54密耳
.NET 4.5(Win7-Win32)=38.47 mil
}
为什么Mono在Linux和BSD上运行
<<< AMD Athlon 64 X2 Dual Core 4600+ 2.40GHz >>>
.NET 4.5 (Win8-Win32)
(x86) = 1.179 sec
(x64) = 1.549 sec

Mono 3.2.3 (Win8-Win32)
(x86) = 2.059 sec
(x64) = 2.07 sec

Mono 3.2.3 (PC-Linux)
(x86) = 2.425
(x64) = 2.409

Mono 3.2.1 (PC-BSD)
(x86) = 2.536
(x64) = 2.509

<<< Intel Core2 Duo P8600 2.40GHz >>>
.NET 4.5 (Win7-Win32)
(x86) = 1.05 sec
(x64) = 1.132 sec

Mono 3.2.3 (Win7-Win32) 
(x86) = 1.692 sec
(x64) = 1.702 sec

Mono 3.2.3 (OSX 10.9)
(x86) = 1.675 sec
(x64) = 1.679 sec