3

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

我在(Win8.1、Linux、BSD 和 OSX)上使用 .NET 4.5 与 Mono 3.2.x 进行了一些性能测试。

注意:这些测试是使用 Mono x86 或 .NET x86 arch 编译的。他们没有在 Virtual Box 中运行。测试计算机使用本地运行的“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();
        }
    }
}

以下是结果

<<< 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
}

为什么 Linux 和 BSD 上的 Mono 运行速度比 Windows 和 OSX 上的 Mono 慢 1/3?

4

2 回答 2

1

最后我检查了一下,Mono 的编译器技术是基于 iburg 的,它在 1983 年很受欢迎(这是我在学校 - 大约 1984 年 - 由于 Davidson&Fraser&Hansen 学到的东西),但它基于固定(通常是乐观的)负载延迟和非- 流水线指令调度。从那时起,我们对这两种算法都有了新的算法(例如: http: //pages.cs.wisc.edu/~fischer/cs701.f08/eggers.pdf - 一篇关于加载延迟的论文,大约在 1992 年,另外,例如基于资源的 CPU 调度现在用于 clang 和 gcc 中的多个问题调度,IIRC,好的论文大约在 2000 年开始出现)。所以这可能解释了那里可能有 5-30% 的性能。

此外,还有 GC 性能:mono 在这方面落后,而 microsoft 正在与非常擅长 JVM 的家伙(IBM、Sun)竞争。我的感觉是,尽管 mono 项目是一项伟大的工作,但它的工作量很大,只是为了跟上微软不断涌入的所有新东西(为编译器、GC 性能、VM 留下很少的时间……改进)。请记住,MS 的大部分新代码都是在 .Net 中编写的,并且对提高速度非常感兴趣(而且他们总是有不错的编译器)。

因此,性能可能不同的原因有很多。再说一次,它是开源的,如果人们足够关心,他们可以做出贡献。

于 2014-05-26T20:58:23.957 回答
1

在此处输入图像描述

好的,我做了一个更好的基准测试(RayTraceBenchmark):https ://github.com/zezba9000/RayTraceBenchmark (随意做拉请求以添加更多语言或发布端口[希望看到结果])

它渲染 3D 场景并保存 RAW 图像文件。您可以通过 Photoshop 或其他方式打开图像。图像的分辨率为 1280x720。

以下是测试的当前结果:https ://github.com/zezba9000/RayTraceBenchmark/blob/master/C%23/Results.md

如您所见,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
于 2013-11-02T06:49:57.997 回答