title: Parse substring to double directly
body: <p>If I have a string such as <code>1 2 3</code> and I identify the position of a substring containing a <code>double</code>, how can I parse it directly from the substring without creating a temporary string?</p>
  
  <p>For example, I could do <code>System.Double.Parse(str.Substring(0, 1))</code> but that would create a temporary string which is slow and needless. Is it possible to parse a double directly from part of the original string?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Eric Lippert has questioned my motives here, stating that "Small strings are cheap". The motivation for this comes from my doing the same thing for the parsing of ints and seeing a massive performance improvements because, apparently, small strings are not so cheap.</p>
  
  <p>Here is a function that lexes a sequence of ints via temporary strings:</p>
  
  <pre><code>let lex f (s: string) =
    let rec inside i0 (s: string, i) =
      if i = s.Length then
        f (s.Substring(i0, i-i0) |&gt; System.Int32.Parse)
      else
        let c = s.[i]
        if '0'&lt;=c &amp;&amp; c&lt;='9' then
          inside i0 (s, i+1)
        else
          f (s.Substring(i0, i-i0) |&gt; System.Int32.Parse)
          outside (s, i)
    and outside (s: string, i) =
      if i &lt; s.Length then
        let c = s.[i]
        if '0'&lt;=c &amp;&amp; c&lt;='9' then
          inside i (s, i)
        else
          outside (s, i+1)
    outside (s, 0)
  </code></pre>
  
  <p>This takes 2.4s to lex 15,625,000 ints from a string.</p>
  
  <p>Here is a version that avoids temporary strings:</p>
  
  <pre><code>let lex f (s: string) =
    let rec inside n (s: string, i) =
      if i = s.Length then f n else
        let c = s.[i]
        if '0'&lt;=c &amp;&amp; c&lt;='9' then
          inside (10*n + int c - int '0') (s, i+1)
        else
          f n
          outside (s, i)
    and outside (s: string, i) =
      if i &lt; s.Length then
        let c = s.[i]
        if '0'&lt;=c &amp;&amp; c&lt;='9' then
          inside 0 (s, i)
        else
          outside (s, i+1)
    outside (s, 0)
  </code></pre>
  
  <p>This takes 0.255s, over 9x faster than the solution that uses temporary strings!</p>
  
  <p>I see no reason why lexing floats should be any different. Therefore, by not providing the ability to parse a float from a substring .NET is leaving an order of magnitude in performance on the table. I do a lot of scientific computing and often have to lex large amounts of data, especially at startup, so I really don't want to throw performance to the wind like this.</p>