title: All .NET assemblies running extremely slow on Windows Server
body: <p>I have very simple trivial C# code compiled to .NET <code>exe</code> assmebly:</p>
  
  <pre><code>using System;
  class Program
  {
      public static void Main()
      {
          Console.WriteLine("test");
      }
  }
  </code></pre>
  
  <p>The compiled assembly runs on my desktop <code>Windows 8.1</code> machine for about <code>0.02</code> seconds every time, but on the production <code>Windows Server 2012 R2</code> it runs for <code>1.05</code> seconds.</p>
  
  <p>Here is the <strong>PowerShell I am testing with</strong>:</p>
  
  <pre><code>$sw = [Diagnostics.Stopwatch]::StartNew()
  for ($i=1; $i -le 10; $i++)
  {
      .\cs.exe
  }
  $sw.Stop()
  $sw.Elapsed
  </code></pre>
  
  <p>Output on my <strong>local machine</strong>:</p>
  
  <pre><code>TotalMinutes      : 0.00391715666666667
  TotalSeconds      : 0.2350294
  TotalMilliseconds : 235.0294
  </code></pre>
  
  <p>.NET versions on my local machine:</p>
  
  <pre><code>PSChildName                                                 Version
  -----------                                                 -------
  v2.0.50727                                                  2.0.50727.4927
  v3.0                                                        3.0.30729.4926
  Windows Communication Foundation                            3.0.4506.4926
  Windows Presentation Foundation                             3.0.6920.4902
  v3.5                                                        3.5.30729.4926
  Client                                                      4.5.51641
  Full                                                        4.5.51641
  Client                                                      4.0.0.0
  </code></pre>
  
  <p>Output on the <strong>server</strong>:</p>
  
  <pre><code>TotalMinutes      : 0.172228565
  TotalSeconds      : 10.3337139
  TotalMilliseconds : 10333.7139
  </code></pre>
  
  <p>.NET version on server:</p>
  
  <pre><code>PSChildName                                                 Version
  -----------                                                 -------
  Client                                                      4.5.51650
  Full                                                        4.5.51650
  Client                                                      4.0.0.0
  </code></pre>
  
  <p>Environment variables on server:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  ALLUSERSPROFILE                C:\ProgramData
  APPDATA                        C:\Users\*********\AppData\Roaming
  CLIENTNAME                     *********
  CommonProgramFiles             C:\Program Files\Common Files
  CommonProgramFiles(x86)        C:\Program Files (x86)\Common Files
  CommonProgramW6432             C:\Program Files\Common Files
  COMPUTERNAME                   *********
  ComSpec                        C:\Windows\system32\cmd.exe
  COR_ENABLE_PROFILING           7
  COR_PROFILER                   {8019fee9-9590-4bd4-b2c9-815628f80f0f}
  CORECLR_ENABLE_PROFILING       0
  FP_NO_HOST_CHECK               NO
  HOMEDRIVE                      C:
  HOMEPATH                       \Users\*********
  JAVA_HOME                      C:\Progra~1\Java\jdk1.8.0_45
  LOCALAPPDATA                   C:\Users\*********\AppData\Local
  LOGONSERVER                    \\*********
  NUMBER_OF_PROCESSORS           12
  OS                             Windows_NT
  Path                           C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System3...
  PATHEXT                        .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL
  PROCESSOR_ARCHITECTURE         AMD64
  PROCESSOR_IDENTIFIER           Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
  PROCESSOR_LEVEL                6
  PROCESSOR_REVISION             3f02
  ProgramData                    C:\ProgramData
  ProgramFiles                   C:\Program Files
  ProgramFiles(x86)              C:\Program Files (x86)
  ProgramW6432                   C:\Program Files
  PSModulePath                   C:\Users\*********\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPow...
  PUBLIC                         C:\Users\Public
  SESSIONNAME                    RDP-Tcp#126
  SystemDrive                    C:
  SystemRoot                     C:\Windows
  TEMP                           C:\Users\ACADWE~1\AppData\Local\Temp\2
  TMP                            C:\Users\ACADWE~1\AppData\Local\Temp\2
  USERDOMAIN                     *********
  USERDOMAIN_ROAMINGPROFILE      *********
  USERNAME                       *********
  USERPROFILE                    C:\Users\*********
  windir                         C:\Windows
  </code></pre>
  
  <p><strong>What can cause this slowness of 1 second?</strong></p>
  
  <p>Is the JIT compiler the problem?</p>
  
  <p>Is there any CLR-related service that should be ran in the server?</p>
  
  <p>Are there any application validation checks that can be disabled?</p>
  
  <p><em>This problem happens only to .NET exe files. Simple executables compiled with C++ run for <code>0.01</code> seconds</em></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've tried to run <code>ngen.exe ExecuteQueuedItems</code> as suggested in the comments but nothing changed. Here is the result from the command:</p>
  
  <pre><code>PS C:\Windows\system32&gt; C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe ExecuteQueuedItems
  Microsoft (R) CLR Native Image Generator - Version 4.0.30319.33440
  Copyright (c) Microsoft Corporation.  All rights reserved.
  All compilation targets are up to date.
  </code></pre>