title: Fastest Way for Converting an Object to Double?
body: <p>What is the fastest way to convert an object to a double? I'm at a piece of code right now, which reads:</p>
  
  <pre><code>var d = double.TryParse(o.ToString(), out d);  // o is the Object...
  </code></pre>
  
  <p>First thoughts were to rewrite this as </p>
  
  <pre><code>var d = Convert.ToDouble(o);
  </code></pre>
  
  <p>but would that actually be faster?</p>
  
  <p><strong>EDIT:</strong>  In addition to running the profile (by the way, I strongly recommend <strong>JetBrains dotTrace</strong> to any developer), I ran Reflector, and that helped me to come up with the following (more or less the relevant portion of the code):</p>
  
  <pre><code>if (o is IConvertible)
  {
      d = ((IConvertible)o).ToDouble(null);
  }
  else
  {
      d = 0d;
  }
  </code></pre>
  
  <p>The original code <code>double.TryParse()</code> executed in 140ms.  The new code executes in 34ms. I'm almost certain that this is the optimization path I should take, but before I do that, does anyone see anything problematic with my "optimized" code?  Thanks in advance for your feedback!</p>