Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does .NET Core 2.0 perform worse than .NET Framework 4.6.1

Tags:

c#

.net-core

I've wrote a program that creates 4 threads which each sort 20.000 numbers from low to high 50 times. I've runned this test several times on .NET Core 2.0 and .NET Framework 4.6.1. In this test .NET Framework always outperforms .NET Core.

Setup

  • .NET Core in release mode & published
  • Windows 10, i7 duo core, 4 threads (hyperthreading)

The following code has been used to benchmark the two frameworks.

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

Test results:

.NET Core

  • Average: 761ms
  • Total time: 38s

.NET Framework

  • Average: 638ms
  • Total time: 32s

.NET Core isn't slower on only CPU related tasks. It's also slower on disk I/O tasks.

Any idea's why .NET Core is a bit slower on this part? Are there changes I can make to improve the performance of .NET Core?

like image 294
Jamie Avatar asked Mar 22 '18 09:03

Jamie


People also ask

Is .NET Core faster than .NET Framework?

. NET Core is faster than . NET Framework because the architecture of . NET Core is written or restructured from scratch to make it a modular, lightweight, fast, and cross-platform Framework.

Should I use .NET Core or .NET Framework?

NET and ASP.NET Core are your best options. The high-performance server runtime for Windows Server and Linux makes ASP.NET Core a top performing web framework on TechEmpower benchmarks. Performance and scalability are especially relevant for microservices architectures, where hundreds of microservices may be running.

What are the disadvantages of .NET Core?

NET Core, but you may still have some issues with compatibility if the class library uses any . NET Framework APIs that are not supported. You cannot use Windows-specific APIs in ASP.NET Core and . NET Core since these frameworks are designed to be more independent from the operating system.

Is .NET Core replacing .NET Framework?

With the planned release of a unified platform in 2020, . NET Core will replace . NET Framework. You will be able to use it to target Windows, Linux, macOS, iOS, Android, tvOS, watchOS, WebAssembly, and more.


1 Answers

.NET Framework projects default to 32-bit code. This option is visible in the build settings of a project and selected by default. .NET Core projects default to 64-bit code. If you untick the "Prefer 32-bit" box you will notice .NET Framework drops in performance.

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.

The answers were provided in the comments by Hans Passant and Jeroen Mostert.

like image 138
Jamie Avatar answered Sep 17 '22 14:09

Jamie