Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:29:43+00:00 2026-06-10T07:29:43+00:00

I was shocked at how slow DateTime.Parse is. This code takes around 100 seconds

  • 0

I was shocked at how slow DateTime.Parse is.
This code takes around 100 seconds to run; if I use regex version it takes 100 milliseconds.
What is going on here?

Stopwatch sw = new Stopwatch();
sw.Start();
var re = new Regex(@"(\d\d)/(\d\d)/(\d\d\d\d) (\d\d):(\d\d):(\d\d)", RegexOptions.Compiled);
for (int i = 0; i < 100000; i++)
{
    //var m = re.Match("08/01/2012 23:10:12");
    DateTime.Parse("08/01/2012 23:10:12", CultureInfo.CreateSpecificCulture("en-US"));
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);

Edit: Mark is right, moving the CultureInfo.CreateSpecificCulture("en-US") outside the loop helped. The reason I didn’t do it before is that I profiled this code with VS Profiler and it showed following result:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:29:45+00:00Added an answer on June 10, 2026 at 7:29 am

    It’s not a fair test.

    1. The call to CultureInfo.CreateSpecificCulture("en-US") is the slow part. Move it outside the loop, store the result and reuse it.

    2. Your regular expression only handles one specific format, but DateTime.Parse can handle many different input formats. It has to decide which of the many format it understands is the correct one to use. If you know in advance what the format is then use DateTime.ParseExact instead of DateTime.Parse.

    The fixed code is as follows:

    CultureInfo ci = CultureInfo.CreateSpecificCulture("en-US");
    for (int i = 0; i < 100000; i++)
    {
        DateTime.ParseExact("08/01/2012 23:10:12", "MM/dd/yyyy HH:mm:ss", ci);
    }
    

    With these two changes, I get that the DateTime.ParseExact and the regular expression approach are almost the same.

    And your regular expression accepts some datetimes that are invalid, such as 00/00/0000 99:99:99. If you fix it so that it only accepts valid datetimes it would be slower.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question for the generic collection gurus. I'm shocked to find that
I'm pretty shocked I can't find an answer to this (maybe there isn't one).
I'm completely shocked that I can't find anything about how to do this. How
Proper object disposal removed for brevity but I'm shocked if this is the simplest
Running the following (slightly pseudo)code produces the following results. Im shocked at how innacurate
Using exceptions for normal flow of code is bad - it's slow, it's bad
I'm a little shocked that (if?) this precise questions hasn't been asked, but 15
First: I'm shocked that I have to ask this question. Nowhere in the docs
This is a strange one, I'm shocked I've never noticed it before. It works
I was kind of shocked by this. Could someone explain why this works? A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.