title: Improve performance custom square root algorithm C#
body: <p>Here is a algorithm I have come up with to calculate the square root, currently when testing it with a loop n times and stopwatch it's about 20-100x slower then C# <code>Math.Sqrt()</code>;</p>
  
  <p>Is there any way to improve the performance of this function or is the performance as good as it gonna get with  this specific algorithm?</p>
  
  <p><strong>My C# square root algorithm:</strong></p>
  
  <pre><code>static class MyMath
  {
      public static double Sqrt(double _d)
      {
          double x = 0;
          double y = 2;
          double z = 1;
          double w = _d;
          double h = 1;
          double t = 0;
          double px = 0;
          int itr = 0;
          while (true)
          {
              w = (w / y);
              h *= y;
              if (h &gt; w)
              {
                  t = w;
                  w = h;
                  h = t;
                  z *= 0.5;
                  y = (1 + z);
              }
              x = ((w + h) * 0.5);
              if (itr &gt;= 100 || w == h || px == x)
              {
                  return (x);
              }
              px = x;
              itr++;
          }
      }
  }
  </code></pre>
  
  <p><strong>How I test the performance:</strong></p>
  
  <pre><code>using System.Diagnostics;
  
  
  Stopwatch sw = new Stopwatch();
  sw.Start();
  for (int i = 0; i &lt; 10000; i++)
  {
      MyMath.Sqrt(2);
  }
  sw.Stop();
  Debug.Print(sw.ElapsedTicks.ToString());
  </code></pre>
  
  <p><strong>EDIT3: Slightly improved version:</strong></p>
  
  <pre><code>    static class MyMath
      {
          public static double Sqrt(double _d)
          {
              double x = 0;
              double y = 2;
              double z = 1;
              double w = _d;
              double h = 1;
              double t = 0;
              double px = 1;
              while (true)
              {
                  if (x == px)
                  {
                      return ((w + h) * 0.5);
                  }
                  if (w &lt; h)
                  {
                      t = w;
                      w = h;
                      h = t;
                      z *= 0.25;
                      y = (z + 1);
                      px = x;
                  }
                  w /= y;
                  h *= y;
                  x = (w + h);
              }
          }
      }
  </code></pre>
  
  <p><strong>EDIT3: Updated Slightly improved version2 + changed benchmark method2</strong>: 
  (<em>Running in Release Mode</em>)</p>
  
  <pre><code>        Stopwatch sw = new Stopwatch();
          int n = 100000;
          double[] squareArr = new double[n];
          Random rng = new Random(1234);
          for (int i = 0; i &lt; n; i++)
          {
              squareArr[i] = rng.Next(1, 100000);
          }
          sw.Start();
          for (int i = 0; i &lt; n; i++)
          {
              squareArr[i] = MyMath.Sqrt(squareArr[i]) ;
          }
          sw.Stop();
          debugBox.AppendText("AverageTime: " + (sw.ElapsedTicks / (double)n).ToString());
  </code></pre>
  
  <p>Currently according to my test the default Math.Sqrt() ~0.086 Ticks and mySqrt() ~4.8 Ticks.</p>
  
  <p><strong>EDIT 4:(Fixed bug: moved px = x in if statement)</strong></p>