Note that there are some explanatory texts on larger screens.

plurals
  1. POFaster alternative to DateTime.ParseExact
    text
    copied!<p>I parse a string into a DateTime millions of times:</p> <pre><code>public static CultureInfo ci = CultureInfo.InvariantCulture; while (!reader.EndOfStream) { line = reader.ReadLine(); string[] fields = line.Split(' '); DateTime newDT = DateTime.ParseExact(fields[0], "yyyyMMddTHHmmssfff", ci); } </code></pre> <p>My profiler highlights ParseExact as being a huge part of time taken. Is there any other method/approach that could parse the string into a DateTime that would be faster?</p> <p>FOLLOW UP1:</p> <p>1) I tried this - but speed was same</p> <pre><code>bool OK = DateTime.TryParseExact(fields[0], "yyyyMMddTHHmmssfff", null, System.Globalization.DateTimeStyles.None,out DT); </code></pre> <p>2)</p> <p>I tried to code my own parser - but this too was as slow:</p> <pre><code>public static DateTime fastParse(ref string s) { return new DateTime(int.Parse(s.Substring(0,4)), int.Parse(s.Substring(4,2)),int.Parse(s.Substring(6,2)), int.Parse(s.Substring(9,2)),int.Parse(s.Substring(11,2)),int.Parse(s.Substring(13,2)),int.Parse(s.Substring(15, 3))); } </code></pre> <p>FOLLOW UP2</p> <p>I tried Master117 suggestion of storing values - AGAIN it is NO faster - perhaps the issue is the construction?</p> <pre><code> public class fastParseData { int year; int mon; int day; int hour; int min; string previousSlice = ""; public DateTime fastParse(ref string s) { if (previousSlice != s.Substring(0, 12)) { year=int.Parse(s.Substring(0,4)); mon=int.Parse(s.Substring(4,2)); day=int.Parse(s.Substring(6,2)); hour= int.Parse(s.Substring(9,2)); min = int.Parse(s.Substring(11,2)); previousSlice = s.Substring(0, 12); } return new DateTime(year, mon, day, hour,min, int.Parse(s.Substring(13, 2)), int.Parse(s.Substring(15, 3))); } } </code></pre> <p>FOLOW UP3</p> <pre><code> public class fastParseData { int year; int mon; int day; int hour; int min; string previousSlice = ""; DateTime previousDT; public DateTime fastParse(ref string s) { if (previousSlice != s.Substring(0, 12)) { year=int.Parse(s.Substring(0,4)); mon=int.Parse(s.Substring(4,2)); day=int.Parse(s.Substring(6,2)); hour= int.Parse(s.Substring(9,2)); min = int.Parse(s.Substring(11,2)); previousSlice = s.Substring(0, 12); previousDT = new DateTime(year, mon, day, hour,min,0,0); } return previousDT.AddMilliseconds((int.Parse(s.Substring(13, 2))*1000)+int.Parse(s.Substring(15, 3))); } } </code></pre> <p>FOLLOW UP4</p> <p>From my profiler the crux seems to be </p> <pre><code>int.Parse(s.Substring(13, 2)) </code></pre> <p>With the Parse bit being more costly than the substring.</p> <p>I tried </p> <pre><code>int.TryParse(s.Substring(13, 2),NumberStyles.None,ci, out secs) Convert.ToInt32(s.Substring(13, 2)); </code></pre> <p>but again - no difference in speed.</p> <p>Is there a faster way to parse an int?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload