title: Why is my C# program faster in a profiler?
body: <p>I have a relatively large system (~25000 lines so far) for monitoring radio-related devices. It shows graphs and such using latest version of ZedGraph. 
  The program is coded using C# on VS2010 with Win7. 
  The problem is:</p>
  
  <ul>
  <li>when I run the program from within VS, it runs slow</li>
  <li>when I run the program from the built EXE, it runs slow</li>
  <li>when I run the program though Performance Wizard / CPU Profiler, it runs Blazing Fast.</li>
  <li>when I run the program from the built EXE, and then start VS and Attach a profiler to ANY OTHER PROCESS, my program speeds up!</li>
  </ul>
  
  <p>I want the program to always run that fast!</p>
  
  <p>Every project in the solution is set to RELEASE, Debug unmanaged code is DISABLED, Define DEBUG and TRACE constants is DISABLED, Optimize Code - I tried either, Warning Level - I tried either, Suppress JIT - I tried either,
  in short I tried all the solutions already proposed on StackOverflow - none worked. Program is slow outside profiler, fast in profiler.
  I don't think the problem is in my code, because it becomes fast if I attach the profiler to other, unrelated process as well!</p>
  
  <p>Please help!
  I really need it to be that fast everywhere, because it's a business critical application and performance issues are not tolerated...</p>
  
  <p><strong>UPDATES 1 - 8 follow</strong></p>
  
  <p><strong>--------------------Update1:--------------------</strong></p>
  
  <p>The problem seems to Not be ZedGraph related, because it still manifests after I replaced ZedGraph with my own basic drawing.</p>
  
  <p><strong>--------------------Update2:--------------------</strong></p>
  
  <p>Running the program in a Virtual machine, the program still runs slow, and running profiler from the Host machine doesn't make it fast.</p>
  
  <p><strong>--------------------Update3:--------------------</strong></p>
  
  <p>Starting screen capture to video also speeds the program up!</p>
  
  <p><strong>--------------------Update4:--------------------</strong></p>
  
  <p>If I open the Intel graphics driver settings window (this thing: <a href="http://www.intel.com/support/graphics/sb/img/resolution_new.jpg" rel="noreferrer">http://www.intel.com/support/graphics/sb/img/resolution_new.jpg</a>)
  and just constantly hover with the cursor over buttons, so they glow, etc, my program speeds up!.
  It doesn't speed up if I run GPUz or Kombustor though, so no downclocking on the GPU - it stays steady 850Mhz.</p>
  
  <p><strong>--------------------Update5:--------------------</strong></p>
  
  <p>Tests on different machines:</p>
  
  <p>-On my Core i5-2400S with Intel HD2000, UI runs slow and CPU usage is ~15%. </p>
  
  <p>-On a colleague's Core 2 Duo with Intel G41 Express, UI runs fast, but CPU usage is ~90% (which isn't normal either)</p>
  
  <p>-On Core i5-2400S with dedicated Radeon X1650, UI runs blazing fast, CPU usage is ~50%.  </p>
  
  <p><strong>--------------------Update6:--------------------</strong></p>
  
  <p>A snip of code showing how I update a single graph (<code>graphFFT</code> is an encapsulation of <code>ZedGraphControl</code> for ease of use):</p>
  
  <pre><code>public void LoopDataRefresh() //executes in a new thread
          {
              while (true)
              {
                  while (!d.Connected)
                      Thread.Sleep(1000);
                  if (IsDisposed)
                      return;
  //... other graphs update here
                  if (signalNewFFT &amp;&amp; PanelFFT.Visible)
                  {
                      signalNewFFT = false;
                      #region FFT
                      bool newRange = false;
                      if (graphFFT.MaxY != d.fftRangeYMax)
                      {
                          graphFFT.MaxY = d.fftRangeYMax;
                          newRange = true;
                      }
                      if (graphFFT.MinY != d.fftRangeYMin)
                      {
                          graphFFT.MinY = d.fftRangeYMin;
                          newRange = true;
                      }
  
                      List&lt;PointF&gt; points = new List&lt;PointF&gt;(2048);
                      int tempLength = 0;
                      short[] tempData = new short[2048];
  
                      int i = 0;
  
                      lock (d.fftDataLock)
                      {
                          tempLength = d.fftLength;
                          tempData = (short[])d.fftData.Clone();
                      }
                      foreach (short s in tempData)
                          points.Add(new PointF(i++, s));
  
                      graphFFT.SetLine("FFT", points);
  
                      if (newRange)
                          graphFFT.RefreshGraphComplete();
                      else if (PanelFFT.Visible)
                          graphFFT.RefreshGraph();
  
                      #endregion
                  }
  //... other graphs update here
                  Thread.Sleep(5);
              }
          }
  </code></pre>
  
  <p><code>SetLine</code> is:</p>
  
  <pre><code>public void SetLine(String lineTitle, List&lt;PointF&gt; values)
      {
          IPointListEdit ip = zgcGraph.GraphPane.CurveList[lineTitle].Points as IPointListEdit;
          int tmp = Math.Min(ip.Count, values.Count);
          int i = 0;
          while(i &lt; tmp)
          {
              if (values[i].X &gt; peakX)
                  peakX = values[i].X;
              if (values[i].Y &gt; peakY)
                  peakY = values[i].Y;
              ip[i].X = values[i].X;
              ip[i].Y = values[i].Y;
              i++;
          }
          while(ip.Count &lt; values.Count)
          {
              if (values[i].X &gt; peakX)
                  peakX = values[i].X;
              if (values[i].Y &gt; peakY)
                  peakY = values[i].Y;
              ip.Add(values[i].X, values[i].Y);
              i++;
          }
          while(values.Count &gt; ip.Count)
          {
              ip.RemoveAt(ip.Count - 1);
          }
      }
  </code></pre>
  
  <p><code>RefreshGraph</code> is:</p>
  
  <pre><code>public void RefreshGraph()
      {
          if (!explicidX &amp;&amp; autoScrollFlag)
          {
              zgcGraph.GraphPane.XAxis.Scale.Max = Math.Max(peakX + grace.X, rangeX);
              zgcGraph.GraphPane.XAxis.Scale.Min = zgcGraph.GraphPane.XAxis.Scale.Max - rangeX;
          }
          if (!explicidY)
          {
              zgcGraph.GraphPane.YAxis.Scale.Max = Math.Max(peakY + grace.Y, maxY);
              zgcGraph.GraphPane.YAxis.Scale.Min = minY;
          }
          zgcGraph.Refresh();
      }
  </code></pre>
  
  <p>.</p>
  
  <p><strong>--------------------Update7:--------------------</strong></p>
  
  <p>Just ran it through the ANTS profiler. It tells me that the <code>ZedGraph</code> refresh counts when the program is fast are <em>precisely</em> two times higher compared to when it's slow.
  Here are the screenshots:
  <img src="https://i.stack.imgur.com/wcqU3.png" alt="screenshot of ANTS when slow">
  <img src="https://i.stack.imgur.com/q0XyU.png" alt="screenshot of ANTS when fast"></p>
  
  <p>I find it VERY strange that, considering the small difference in the length of the sections, performance differs twice with mathematical precision.</p>
  
  <p>Also, I updated the GPU driver, that didn't help.</p>
  
  <p><strong>--------------------Update8:--------------------</strong></p>
  
  <p>Unfortunately, for a few days now, I'm unable to reproduce the issue... I'm getting constant acceptable speed (which still appear a bit slower than what I had in the profiler two weeks ago) which isn't affected by any of the factors that used to affect it two weeks ago - profiler, video capturing or GPU driver window. I still have no explanation of what was causing it... </p>