title: Code runs 30% slower in .Net Core compared to .Net Framework - any way to speed things up?
body: <h2>Background</h2>
  <p>We are currently going though the process of converting our codebase from .Net Framework 4.8 to .Net Core 3.1.</p>
  <p>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.</p>
  <h2>To reproduce</h2>
  <p>I created a multitargeted SDK-style project as follows:</p>
  <pre><code>&lt;Project Sdk=&quot;Microsoft.NET.Sdk&quot;&gt;
    &lt;PropertyGroup&gt;
      &lt;OutputType&gt;Exe&lt;/OutputType&gt;
      &lt;TargetFrameworkS&gt;net48;netcoreapp3.1&lt;/TargetFrameworkS&gt;
      &lt;Optimize&gt;true&lt;/Optimize&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition=&quot;'$(Configuration)|$(Platform)'=='Release|AnyCPU'&quot;&gt;
      &lt;PlatformTarget&gt;x86&lt;/PlatformTarget&gt;
    &lt;/PropertyGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  <p>The code for this project is as follows (the important code is inside the <code>for (int iter = ...</code> loop):</p>
  <pre><code>using System;
  using System.Diagnostics;
  
  namespace FooBar
  {
      class Program
      {
          static void Main()
          {
  #if NET48
              Console.WriteLine(&quot;NET48: Is 64 bits = &quot; + Environment.Is64BitProcess);
  #elif NETCOREAPP3_1
              Console.WriteLine(&quot;NETCOREAPP3_1: Is 64 bits = &quot; + 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 &lt; 100; ++trial)
              {
                  sum(array);
              }
  
              Console.WriteLine(&quot;Average ms for calls to sum() = &quot; + sw.ElapsedMilliseconds/100);
              Console.ReadLine();
          }
  
          static double sum(double[] array)
          {
              double s = 0;
  
              for (int i = 0; i &lt; array.Length; ++i)
              {
                  s += array[i];
              }
  
              return s;
          }
      }
  }
  </code></pre>
  <h2>Results</h2>
  <p>Timing a <em>release</em> x86 build for .Net Core 3.1 and .Net Framework 4.8 I get the following results:</p>
  <p><strong>.Net Core 3.1:</strong></p>
  <pre><code>NETCOREAPP3_1: Is 64 bits = False
  Average ms for calls to sum() = 122
  </code></pre>
  <p><strong>.Net Framework 4.8:</strong></p>
  <pre><code>NET48: Is 64 bits = False
  Average ms for calls to sum() = 96
  </code></pre>
  <p>Thus the .Net Core 3.1 results are around 30% slower than .Net Framework 4.8.</p>
  <p><strong>NOTE:</strong> This only affects the x86 build. For an x64 build, the times are similar between .Net Framework and .Net Core.</p>
  <p>I find this most disappointing, particularly since I thought that .Net Core would be likely to have better optimization ...</p>
  <p>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?</p>
  <hr />
  <p>[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.</p>
  <p>With this edit, I am basically just timing how long it takes to sum all 100,000,000 elements of a large double[] array.</p>
  <p>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.</p>
  <p>However, given that other people cannot reproduce this, I will take Lex Li's advice and post this on the Microsoft github page.</p>