Which is Quicker: DateTime.TryParse or Regex
In .NET, to determine whether a supplied string is a date, which one is quicker: using DateTime to cast it to a date, or using a regular expression to check the string for its validity as a date?
I just need to ensure the value supplied i开发者_StackOverflow中文版s a date, and I'm not doing anything with it afterward.
Thanks.
My first question would be which is more expressive? Or Which is the most readable? In a case like this where performance gains would probably be negligible, I'd vote for the code that's the easiest to maintain/read.
EDIT
Found a decent, similar post. It's worth a read
Regex vs Tryparse what is the best in performance
A good regex should be much faster, and maybe consume less transient memory.
But here's the flipside of the coin:
You're pretty-much tied to only one time format, which means that internationalization will be painful, and that your users need to be educated to enter the date in the proper format.
Also, you will lose some date validation, say, how do you weed-out Feb 29th on non leap-years? April 31st?
The best thing to do would be to write a bit of test code for both and then run a loop to do it a million times. Without knowing the input, it would be hard to answer this (although my guess would be that TryParse would be quicker).
That said, the time difference on today's processors is probably irrelevant.
UPDATE: When run in a fiddle TryParse is a lot quicker.
I ran a rudimentary test with 10000 items. It looks like Regexp is at least twice as fast as DateTime.Parse. See for yourself with the code below:
private string[] arrDates = new string[10000];
protected void Page_Load(object sender, EventArgs e)
{
initialise();
RunRegexDemo();
RunDateTimeParseDemo();
}
private void initialise()
{
Random ryear, rmonth, rdate;
ryear = new Random();
rmonth = new Random();
rdate = new Random();
int y, m, d;
DateTime dt;
for (int i = 0; i < arrDates.Length; i++)
{
y = 0;
m = 0;
d = 0;
while (y < 1850)
{
y = ryear.Next(2050);
}
while (m < 1 || m > 12)
{
m = rmonth.Next(12);
}
while (d < 1 || d > 28)
{
d = rdate.Next(28);
}
dt = new DateTime(y, m, d);
arrDates[i] = dt.ToString("yyyy-MM-dd");
//lbl1.Text += "<br />" + arrDates[i];
}
}
private void RunRegexDemo()
{
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
lbl1.Text+= "<h4>Starting Regex demo</h4>";
string f;
st.Start();
foreach(string x in arrDates){
f= "<br/>" + x + " is a valid date? = " + System.Text.RegularExpressions.Regex.IsMatch(x, @"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$");
}
st.Stop();
lbl1.Text+= "<p>Ended RegEx demo. Elapsed time: " + st.ElapsedMilliseconds;
}
protected void RunDateTimeParseDemo(){
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
lbl1.Text += "<h4>Starting DateTime.Parse demo</h4>";
st.Start();
DateTime dt;
string f;
foreach (string x in arrDates)
{
f = "<br/>" + x + " is a valid date? = " + DateTime.TryParse(x, out dt);
}
st.Stop();
lbl1.Text += "<p>Ended TryParse demo. Elapsed time: " + st.ElapsedMilliseconds;
}
Regex seems to be faster in this case as Regex will only look for patterns where as DateTime parse will need to find the pattern as well as get values out of that pattern to create DateTime object
精彩评论