c# - 为什么 .NET Core 2.0 的性能比 .NET Framework 4.6.1 差

标签 c# .net-core

我编写了一个创建 4 个线程的程序,每个线程将 20.000 个数字从低到高排序 50 次。我已在 .NET Core 2.0 和 .NET Framework 4.6.1 上多次运行此测试。在此测试中,.NET Framework 始终优于 .NET Core。

设置

  • .NET Core 处于 Release模式并已发布
  • Windows 10,i7 双核,4 线程(超线程)

以下代码已用于对两个框架进行基准测试。

static void Main()
    {
        const int amountParallel = 4;
        var globalStopwatch = new Stopwatch();

        globalStopwatch.Start();

        var tasks = new Task<double[]>[4];

        for (int i = 0; i < amountParallel; i++)
        {
            tasks[i] = Start();
        }

        Task.WaitAll(tasks);

        globalStopwatch.Stop();

        Console.WriteLine("Averages: {0}ms", tasks.SelectMany(r => r.Result).Average(x => x));
        Console.WriteLine("Time completed: {0}", globalStopwatch.Elapsed.TotalMilliseconds);
    }

    private static Task<double[]> Start()
    {
        return Task.Factory.StartNew(() =>
        {
            var numbersToSort = new int[20000];

            var globalStopwatch = new Stopwatch();
            var individualStopwatch = new Stopwatch();
            var stopwatchTimes = new double[50];
            int temp;

            globalStopwatch.Start();

            for (int i = 0; 50 > i; i++)
            {
                Console.WriteLine("Running task: {0}", i);
                numbersToSort = Enumerable.Range(0, 20000).Reverse().ToArray();
                individualStopwatch.Start();

                for (int indexNumberArray = 0; numbersToSort.Length > indexNumberArray; indexNumberArray++)
                {
                    for (int sort = 0; numbersToSort.Length - 1 > sort; sort++)
                    {
                        if (numbersToSort[sort] > numbersToSort[sort + 1])
                        {
                            temp = numbersToSort[sort + 1];
                            numbersToSort[sort + 1] = numbersToSort[sort];
                            numbersToSort[sort] = temp;
                        }
                    }
                }

                individualStopwatch.Stop();

                Console.WriteLine("Task {0} completed, took: {1}ms", i, Math.Round(individualStopwatch.Elapsed.TotalMilliseconds));

                stopwatchTimes[i] = individualStopwatch.Elapsed.TotalMilliseconds;

                individualStopwatch.Reset();
            }

            globalStopwatch.Stop();

            Console.WriteLine("Total time: {0}s", Math.Round(globalStopwatch.Elapsed.TotalSeconds, 2));
            Console.WriteLine("Average: {0}ms", Math.Round(stopwatchTimes.Average(time => time)));

            return stopwatchTimes;
        }, TaskCreationOptions.LongRunning);
    }

测试结果:

.NET 核心

  • 平均:761 毫秒
  • 总时长:38s

.NET 框架

  • 平均:638 毫秒
  • 总时长:32s

.NET Core 并不只在 CPU 相关任务上变慢。它在磁盘 I/O 任务上也较慢。

知道为什么 .NET Core 在这部分有点慢吗?我可以进行哪些更改来提高 .NET Core 的性能?

最佳答案

.NET Framework 项目默认为 32 位代码。此选项在项目的build设置中可见,默认情况下处于选中状态。 .NET Core 项目默认为 64 位代码。如果取消选中“首选 32 位”框,您会注意到 .NET Framework 的性能下降。

Another point of note is that the desktop x86 JIT is a separate code base from the x64 JIT. For 64-bit, both .NET Framework and .NET Core use RyuJIT now; for 32-bit .NET Core still uses RyuJIT, but .NET Framework uses the legacy JIT, so you've got both different bitness and a different jitter.

Hans Passant 和 Jeroen Mostert 在评论中提供了答案。

关于c# - 为什么 .NET Core 2.0 的性能比 .NET Framework 4.6.1 差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425406/

相关文章:

.net-core - 在 Amazon SQS 传输上发布的 NServiceBus 路由器事件不由 Azure 服务总线传输终端节点处理

c# - 多行文本框到数组 C#

c# - 模拟只读索引器属性

c# - 如何使用 RabbitMQ 使消费者具有弹性?

sql-server - Entity Framework 核心-错误: keyword not supported: '"server'

dll - 在 .Net Core 2.1 (Windows) 中加载 native 库

c# - 如何在 ASP.NET Web API 中获取依赖解析器的实例

c# - 你能解释一下这个 Math.Log10 与 BigInteger.Log10 的行为吗?

c# - 使用 DRY 和分而治之原则链接的异步方法

Sonarqube 支持 ‘.Net Core’(扩展名为 .xproj)项目