title: Speed up MSBuild build process
body: <p>My Buildprocess works fine but it takes nearly 3 seconds for a really small project.</p>
  
  <p><strong>How could i speed up the build process?</strong></p>
  
  <p>The logfile is to big for posting it. Feel free to ask questions about the logger output, i will then post the requested part.</p>
  
  <p><strong>C# Methods to build the project</strong></p>
  
  <pre><code>private void CompileCode()
          {
              using (var buildManager = new BuildManager())
              {
                  var result = buildManager.Build(this.CreateBuildParameters(), this.CreateBuildRequest());
  
                  if (result.OverallResult == BuildResultCode.Failure)
                  {
                      // Error handling
                      var stringbuilder = new StringBuilder();
  
                      using (var reader = new StreamReader(NameDebuggerLogFile))
                      {
                          stringbuilder.Append(reader.ReadToEnd());
                      }
  
                      throw new CompilerException(stringbuilder.ToString());
                  }
              }
          }
  
          private BuildParameters CreateBuildParameters()
          {
              var projectCollection = new ProjectCollection();
              var buildLogger = new FileLogger { Verbosity = LoggerVerbosity.Detailed, Parameters = "logfile=" + NameDebuggerLogFile };
              var buildParameters = new BuildParameters(projectCollection) { Loggers = new List&lt;ILogger&gt;() { buildLogger } };
              return buildParameters;
          }
  
          private BuildRequestData CreateBuildRequest()
          {
              var globalProperties = new Dictionary&lt;string, string&gt;();
              var buildRequest = new BuildRequestData(FolderPath + NameProjectFile, globalProperties, null, new[] { "Build" }, null, BuildRequestDataFlags.ReplaceExistingProjectInstance);
              return buildRequest;
          }
  </code></pre>
  
  <p><strong>Project file used for the build process</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /&gt;
    &lt;PropertyGroup&gt;
      &lt;AssemblyName&gt;GeneratedFlexForm&lt;/AssemblyName&gt;
      &lt;OutputPath&gt;DLL&lt;/OutputPath&gt;
      &lt;OutputType&gt;Library&lt;/OutputType&gt;
      &lt;DebugType&gt;none&lt;/DebugType&gt;
      &lt;DebugSymbols&gt;false&lt;/DebugSymbols&gt;
      &lt;Optimize&gt;false&lt;/Optimize&gt;
    &lt;/PropertyGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Reference Include="System" /&gt;
      &lt;Reference Include="System.Xml" /&gt;
      &lt;Reference Include="System.Xml.Linq" /&gt;
      &lt;Reference Include="System.ServiceModel" /&gt;
      &lt;Reference Include="System.Runtime.Serialization" /&gt;
      &lt;Reference Include="System.Web" /&gt;
      &lt;Reference Include="System.Web.Services" /&gt;
      &lt;Reference Include="System.Drawing" /&gt;
      &lt;Reference Include="System.Design" /&gt;
      &lt;Reference Include="System.Data" /&gt;
      &lt;Reference Include="System.Core" /&gt;
      &lt;Reference Include="System.Management" /&gt;
      &lt;Reference Include="System.Configuration" /&gt;
      &lt;Reference Include="System.Xaml"&gt;
        &lt;RequiredTargetFramework&gt;4.0&lt;/RequiredTargetFramework&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="WindowsBase" /&gt;
      &lt;Reference Include="PresentationCore" /&gt;
      &lt;Reference Include="PresentationFramework" /&gt;
      &lt;!--&lt;Reference Include="FlexForms.Core" /&gt;--&gt;
      &lt;Reference Include="FlexForms.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"&gt;
        &lt;SpecificVersion&gt;False&lt;/SpecificVersion&gt;
        &lt;HintPath&gt;Library\FlexForms.Core.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="Gizmox.WebGUI.Forms"&gt;
        &lt;SpecificVersion&gt;False&lt;/SpecificVersion&gt;
        &lt;HintPath&gt;Library\Gizmox.WebGUI.Forms.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="Gizmox.WebGUI.Common"&gt;
        &lt;SpecificVersion&gt;False&lt;/SpecificVersion&gt;
        &lt;HintPath&gt;Library\Gizmox.WebGUI.Common.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
      &lt;Reference Include="FlexForms.ServiceProviders"&gt;
        &lt;SpecificVersion&gt;False&lt;/SpecificVersion&gt;
        &lt;HintPath&gt;Library\FlexForms.ServiceProviders.dll&lt;/HintPath&gt;
      &lt;/Reference&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;Page Include="FlexForm.xaml"&gt;
        &lt;Generator&gt;MSBuild:Compile&lt;/Generator&gt;
        &lt;DependentUpon&gt;RadioButtonValueConverter.cs&lt;/DependentUpon&gt;
        &lt;SubType&gt;Designer&lt;/SubType&gt;
      &lt;/Page&gt;
      &lt;Compile Include="FlexForm.xaml.cs"&gt;
        &lt;DependentUpon&gt;FlexForm.xaml&lt;/DependentUpon&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="UserCode.cs"&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
      &lt;Compile Include="RadioButtonValueConverter.cs"&gt;
        &lt;SubType&gt;Code&lt;/SubType&gt;
      &lt;/Compile&gt;
      &lt;Resource Include="datacontext.xml"/&gt;
    &lt;/ItemGroup&gt;
    &lt;Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p><strong>Logger output collected during the build</strong> (just the last lines due to file size)</p>
  
  <blockquote>
    <p>Done building project "projectfile.csproj".</p>
    
    <p>Build succeeded.
        0 Warning(s)
        0 Error(s)</p>
    
    <p>Time Elapsed 00:00:02.68</p>
  </blockquote>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I hope i'm allowed to quote <strong>Seva Titov</strong> here:</p>
  
  <blockquote>
    <p>Building WPF is really tricky, for example XAML files get re-compiled
    twice (see MSDN). You cannot easily replicate it with just csc.exe. If
    you have XAML in your project, you have to use MSBuild. – Seva Titov</p>
  </blockquote>
  
  <p>Thats why i have to stick to MSBuild.</p>
  
  <p>Anyway, i had a few ideas to improve the performance:</p>
  
  <p><em>Problem 1:</em>
  This is a snippet from my logfile, which says that MSBuild will look after .winmd, .dll and .exe files whiles resolving the dependencies.</p>
  
  <blockquote>
  <pre><code>AllowedAssemblyExtensions:
      .winmd
      .dll
      .exe
  </code></pre>
  </blockquote>
  
  <p>Since i have only .dll files i would like to disable that MSBuild considers .winmd files which is the case for nearly all of my dependencies. (example below)</p>
  
  <blockquote>
    <p>Primary reference "PresentationCore, Version=4.0.0.0, Culture=neutral,
    PublicKeyToken=31bf3856ad364e35".
            Resolved file path is "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\PresentationCore.dll".
            Reference found at search path location "{TargetFrameworkDirectory}".
                For SearchPath "{TargetFrameworkDirectory}".
                Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\PresentationCore.winmd",
    but it didn't exist.</p>
  </blockquote>
  
  <p><em>Solution: 1</em>
  I had to modify my CreateBuildRequest() method:</p>
  
  <pre><code>private BuildRequestData CreateBuildRequest()
  {
      var globalProperties = new Dictionary&lt;string, string&gt;();
      // Related to idea 1           
      globalProperties.Add("ExpandSDKAllowedReferenceExtensions", ".dll");
      globalProperties.Add("AllowedReferenceAssemblyFileExtensions", ".dll");
      globalProperties.Add("AllowedReferenceRelatedFileExtensions", string.Empty);
      // Related to idea 2     
      globalProperties.Add("BuildProjectReferences", "false");
      globalProperties.Add("BuildInParallel", "true");
      var buildRequest = new BuildRequestData(FolderPath + NameProjectFile, globalProperties, "4.0", new[] { "Build" }, null, BuildRequestDataFlags.IgnoreExistingProjectState);
      return buildRequest;
  }
  </code></pre>
  
  <p><strong>EDIT2:</strong>
  <em>Idea 2:</em>
  does anyone know how to include the /maxcpucount or /m switch to build my project in parallel?
  its explained on this <a href="http://msdn.microsoft.com/library/vstudio/bb651793" rel="nofollow">MSDN page</a> but i can't figure out how to apply it to the project file listed above </p>
  
  <p><em>Solution 2:</em>
  See <em>Solution 1</em>, but it didn't change much.</p>
  
  <p><strong>EDIT3:</strong>
  After hours of playing around, i guess i will have to give up. The only option i see is to reduce the dependencies in general. But i will come back on this later.</p>
  
  <p>A bit unrelated:</p>
  
  <pre><code>&gt; Task Performance Summary:
  &gt;         0 ms  CreateItem                                 1 calls
  &gt;         0 ms  AssignCulture                              1 calls
  &gt;         0 ms  CallTarget                                 2 calls
  &gt;         0 ms  FindAppConfigFile                          1 calls
  &gt;         0 ms  Delete                                     4 calls
  &gt;         0 ms  GetFrameworkPath                           1 calls
  &gt;         0 ms  ConvertToAbsolutePath                      1 calls
  &gt;         0 ms  MakeDir                                    1 calls
  &gt;         0 ms  Message                                    5 calls
  &gt;         0 ms  AssignTargetPath                           6 calls
  &gt;         1 ms  FindUnderPath                              5 calls
  &gt;         1 ms  FileClassifier                             1 calls
  &gt;         1 ms  RemoveDuplicates                           2 calls
  &gt;         1 ms  CreateCSharpManifestResourceName           1 calls
  &gt;         1 ms  ReadLinesFromFile                          1 calls
  &gt;        15 ms  ResolveAssemblyReference                   1 calls
  &gt;        21 ms  ResourcesGenerator                         1 calls
  &gt;        56 ms  Copy                                       2 calls
  &gt;       299 ms  GenerateTemporaryTargetAssembly            1 calls
  &gt;       502 ms  Csc                                        2 calls
  &gt;       725 ms  MarkupCompilePass1                         1 calls
  &gt;       814 ms  MarkupCompilePass2                         1 calls
  &gt; 
  &gt; Build succeeded.
  &gt;     0 Warning(s)
  &gt;     0 Error(s)
  &gt; 
  &gt; Time Elapsed 00:00:02.27
  </code></pre>