0

I changed culture to Persian in my PC, after that my app load database slower about five times because of this line:

Convert.ToDateTime(someString)

what is problem? and what I should do to fix it?

UPD1: example of string 2010-08-03 00:00:00.000

UPD2: I measure with this following code:

        string s = "2010-08-03 00:00:00.000";
        DateTime start = DateTime.Now;
        int n = (int)5e6;
        for (int i = 0; i < n; i++)
        {
            DateTime date = Convert.ToDateTime(s);
        }
        DateTime finish = DateTime.Now;
        Console.WriteLine(finish - start);
        Console.ReadKey();

in default(AD) calendar:(~5 seconds)

00:05:48

and when I change calendar to Persian in windows 10:(~4 minutes)

03:51:73

Mojtaba Khooryani
  • 1,763
  • 1
  • 17
  • 36
  • how does you change culture? – Rahul Hendawe Sep 28 '16 at 08:58
  • @RahulHendawe in control panel. not in my code! – Mojtaba Khooryani Sep 28 '16 at 08:59
  • 1
    What is that "someString". give an example. Do you want to convert a persian date into AD or what? – Inside Man Sep 28 '16 at 08:59
  • @Stranger updated the question. and no I just want to load datetime in AD but shows in charts in Persian. – Mojtaba Khooryani Sep 28 '16 at 09:01
  • try this DateTime myDate = DateTime.Parse(dateString); – Inside Man Sep 28 '16 at 09:05
  • take a look at http://stackoverflow.com/questions/15738608/converting-dd-mm-yyyy-formatted-string-to-datetime – Inside Man Sep 28 '16 at 09:13
  • How much _slow_ we are talking about exactly? _How_ do you measure it? Can you please show [MCVE] of that performance benchmark? I don't think changing a `CultureInfo` effects your program performance _in any way_. – Soner Gönül Sep 28 '16 at 14:50
  • @SonerGönül updated question. check it. – Mojtaba Khooryani Sep 29 '16 at 06:38
  • Farsi is not exactly blessed with a simple calendar, the start of a new year is a different Gregorian date every year. The vernal equinox (aka spring) signals the start of the year, it moves between March 19th and 21 depending on the year. I'd assume [this graph](https://en.wikipedia.org/wiki/Solar_Hijri_calendar#Accuracy) is built into the code, at least for verification, that can't be that cheap. – Hans Passant Sep 29 '16 at 07:43

1 Answers1

1

It is not documented, so this can be wrong at anytime, but I am guessing that DateTime.Parse (or Convert.ToDateTime) will try all available formats(in current culture) to parse the string. And it just happens that it starts from the format that does not match with your string. Therefore, several failures before getting to the right one. Which results to longer time to complete the process.

To get exact (well approximately) same performance I would recommend using DateTime.ParseExact or DateTime.TryParseExact and specify the Culture Invariant format. For ex:

string s = "2010-08-03 00:00:00.000";
string format = "yyyy-MM-dd HH:mm:ss.fff";
DateTime date = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture);

Reference: Is DateTime.ParseExact() faster than DateTime.Parse() . But note that the accepted answer is not correct.

Community
  • 1
  • 1
kurakura88
  • 2,185
  • 2
  • 12
  • 18