tags:

views:

349

answers:

3

I would like to know if ParseExact is faster than Parse.

I think that it should be ParseExact since you already gave the format but I also think all the checking for the Culture info would slow it down. Does microsoft say in any document on performance difference between the two. The format to be used is a generic 'yyyy/MM/dd' format .

For example:

DateTime.Parse(DateText);
DateTime.ParseExact(DateText, "yyyy/MM/dd", CultureInfo.InvariantCulture);
+5  A: 

You are asking for the difference in speed for two methods that are functionally different (close but still different).

Just pick the one that is most appropriate.

And no, performance of library methods is almost never documented.

But I can tell you something about that difference:

  • it is small,
  • it might change in future versions
Henk Holterman
I am not sure they are functionally different. I inspected the code for both methods and internally they use the same procedures. The only difference I could find is ParseExact uses the formats you specify instead of all the formats for a CultureInfo.
AMissico
@AMissico yes, that's exactly why they are functionally different.
Sander
@Sander: hmm, I see your point. To me, I understand functionally as a change in how something works. In this case, the only difference is the arguments passed to the methods. Internally, it is working the same.
AMissico
+3  A: 

here´s a small comparison of the datetime parsing methods and their performance

Joachim Kerschbaumer
I do not agree with the posted results because there is no indication of how he called `TryParseExact`, and the performance code is not posted for review. My understanding is that if you call `TryParseExact` with the same number of formats as `TryParse` uses the performance will be the same.
AMissico
Agreed, the results are nonsense. TryParse actually calls TryParseExact with absolute minimum overhead. Same thing for Parse vs TryParse, they match however. The vertical scale is *very* misleading.
Hans Passant
A: 

If you specify only one format forTryParseExact that is all it will try. Parse tries all formats either until a best match is found or the first match is found. (I am not sure which.) I did this a few weeks ago, with a customized CultureInfo. I did not test performance, but I did run unit tests on my parse methods (without the customized CultureInfo, see below) against 61,880 dates stored in a database. I did not notice any performance issues.

Regardless if you specify a CultureInfo or not, the internal parsing routines will use CultureInvariant if none is passed. Therefore, CultureInfo does not slow down the process. (There are some performance hits for Hebrew and other "exotic" dates due to extra parsing they require.) From my review of the source code for DateTime, the number of string formats determines how fast these routines can parse a date string. The more format, the slower. If you are only specifying one, then the parsing is as fast as it can be with the ...Exact methods.


Imports System.Globalization

Public Class ExifDateTime

    Private Shared _formats() As String = New String() { _
        "yyyy:MM:dd", _
        "yyyy:MM:dd HH:mm:ss", _
        "yyyy:MM:dd HH:mm:ss.f", _
        "yyyy:MM:dd HH:mm:ss.ff", _
        "yyyy:MM:dd HH:mm:ss.fff", _
        "yyyy:MM:dd HH:mm:ss.fffK", _
        "yyyy:MM:dd HH:mm:ss.ffffffK", _
        "yyyy:MM:dd HH:mm:ssK", _
        ""}


    Public Shared Function Parse(ByVal s As String) As Date
        Dim oResult As Date
        If TryParse(s, DateTimeStyles.None, oResult) = False Then
            Throw New FormatException
        End If
        Return oResult
    End Function

    Public Shared Function Parse(ByVal s As String, ByVal style As System.Globalization.DateTimeStyles) As Date
        Dim oResult As Date
        If TryParse(s, style, oResult) = False Then
            Throw New FormatException
        End If
        Return oResult
    End Function

    Public Shared Function TryParse(ByVal s As String, ByRef result As Date) As Boolean
        Return TryParse(s, DateTimeStyles.None, result)
    End Function

    Public Shared Function TryParse(ByVal s As String, ByVal style As System.Globalization.DateTimeStyles, ByRef result As Date) As Boolean
        Dim fResult As Boolean
        Dim oResultant As Date

        fResult = Date.TryParseExact(s, _formats, CultureInfo.InvariantCulture, style, oResultant)

        If fResult Then
            result = oResultant
        End If

        Return fResult

    End Function

End Class
AMissico