Background
We are currently going though the process of converting our codebase from .Net Framework 4.8 to .Net Core 3.1.
Some of the code is very performance-sensitive. One example is some code that applies a Hamming window filter; I was somewhat dismayed to discover that the .Net Core 3.1-compiled code runs around 30% more slowly than the same code compiled for .Net Framework 4.8.
To reproduce
I created a multitargeted SDK-style project as follows:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkS>net48;netcoreapp3.1</TargetFrameworkS>
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
</Project>
The code for this project is as follows (the important code is inside the for (int iter = ...
loop):
using System;
using System.Diagnostics;
namespace FooBar
{
class Program
{
static void Main()
{
#if NET48
Console.WriteLine("NET48: Is 64 bits = " + Environment.Is64BitProcess);
#elif NETCOREAPP3_1
Console.WriteLine("NETCOREAPP3_1: Is 64 bits = " + Environment.Is64BitProcess);
#else
Invalid build, so refuse to compile.
#endif
double[] array = new double[100_000_000];
var sw = Stopwatch.StartNew();
for (int trial = 0; trial < 100; ++trial)
{
sum(array);
}
Console.WriteLine("Average ms for calls to sum() = " + sw.ElapsedMilliseconds/100);
Console.ReadLine();
}
static double sum(double[] array)
{
double s = 0;
for (int i = 0; i < array.Length; ++i)
{
s += array[i];
}
return s;
}
}
}
Results
Timing a release x86 build for .Net Core 3.1 and .Net Framework 4.8 I get the following results:
.Net Core 3.1:
NETCOREAPP3_1: Is 64 bits = False
Average ms for calls to sum() = 122
.Net Framework 4.8:
NET48: Is 64 bits = False
Average ms for calls to sum() = 96
Thus the .Net Core 3.1 results are around 30% slower than .Net Framework 4.8.
NOTE: This only affects the x86 build. For an x64 build, the times are similar between .Net Framework and .Net Core.
I find this most disappointing, particularly since I thought that .Net Core would be likely to have better optimization ...
Can anyone suggest a way to speed up the .Net Core output so that it is in the same ballpark as .Net Framework 4.8?
[EDIT] I've updated the code and the .csproj to the latest version I'm using for testing. I added some code to indicate which target and platform is running, just to be certain the right version is being run.
With this edit, I am basically just timing how long it takes to sum all 100,000,000 elements of a large double[] array.
I can reproduce this on both my PCs and my laptop, which are running the latest Windows 10 and Visual Studio 2019 installations + latest .Net Core 3.1.
However, given that other people cannot reproduce this, I will take Lex Li's advice and post this on the Microsoft github page.