DateTime parsing performance

30.July.08 – 11:10

In one of my current projects i have to convert a lot of strings to DateTime structures. Sometimes tens of thousands datetime-string arrive at the service per second. As performance is an issue in this project, i was looking for the most performant way to do all the stuff. that’s why i put on a little test to evaluate performance of the different DateTime parsing methods.

Our 4 candidates are :

  • DateTime.Parse
  • DateTime.ParseExact
  • DateTime.TryParse
  • DateTime.TryParseExact

I also wanted to know if there is a difference when using different IFormatProviders. To benchmark i used the InvariantCulture, a concrete CultureInfo (”de-DE” in this case as i could not see any difference between “de-DE”,”en-US”,”fr-FR”,…) and no IFormatProvider for the Parse/TryParse methods (having an IFormatProvider is mandatory for the ***Exact methods).
The following chart shows the result for 10.000.000 calls to every one of those methods.

DateTime parsing results

I was kinda surprised that TryParse was not faster than Parse. But overall the results showed what i expected. TryParseExact is by far the fastest of them all. So , if you care about performance and have to parse a lot of strings into a DateTime structure, use TryParseExact(). 

Comments are welcome!

Edit:I was pointed out to the System.Convert.ToDateTime() Method, but as it uses DateTime.Parse() internally, performance is the same as DateTime.Parse(), so TryParseExact is still the way to go.

Post a Comment

Comment spam protected by SpamBam