Home > .NET Framework, C#, Localization, Optimization > DateTime.Parse Performance Data

DateTime.Parse Performance Data

The potential performance pitfalls of DateTime.Parse are well discussed. What I don’t see as often: actual performance test data.

So, I’m contributing my own test data to the discussion. If you want to see how the data was collected, and test yourself, you can download the dflat script below. Don’t have a way to run it? Download and install Blaze, paste in the script, and hit F5!

Test Results:

Parse Exact, Null Culture          Average over 1,000,000 runs: 37.216077 
Parse Exact, Invariant Culture     Average over 1,000,000 runs: 37.615833 
Basic Parse                        Average over 1,000,000 runs: 38.030132 
Parse Exact, Current Culture       Average over 1,000,000 runs: 38.741877 
Parse, Invariant Culture           Average over 1,000,000 runs: 40.978074 
Parse, Null Culture                Average over 1,000,000 runs: 44.051966 
Parse, Current Culture             Average over 1,000,000 runs: 45.302063

 

Test Script:

// Description:
//    Tests the performance of difference string parsing calls.
// Author:
//    Zachary Gramana
// Created On:
//    2/22/2012
// Imports:
//    - System.Globalization

var runCount = 1000000;

Func<Func<long>,double> runTest = (testMethod)=>
{
    var results = new long[runCount];
    for(var run=0;run<runCount;run++)
    {
        results[run] = testMethod();
    }
    return results.Average();
};

// ############### TEST METHODS ################

Func<long> parseExactNullCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.ParseExact(
            timeString,
            "o",
            null,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();
    
    return sw.ElapsedTicks;
};

Func<long> parseExactCurrentCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.ParseExact(
            timeString,
            "o",
            CultureInfo.CurrentCulture,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();

    return sw.ElapsedTicks;
};

Func<long> parseExactInvariantCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.ParseExact(
            timeString,
            "o",
            CultureInfo.InvariantCulture,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();

    return sw.ElapsedTicks;
};

Func<long> parseBasic = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.Parse(timeString);

    sw.Stop();

    return sw.ElapsedTicks;
};

Func<long> parseNullCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.Parse(
            timeString,
            null,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();

    return sw.ElapsedTicks;
};

Func<long> parseCurrentCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.Parse(
            timeString,
            CultureInfo.CurrentCulture,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();

    return sw.ElapsedTicks;
};

Func<long> parseInvariantCulture = ()=>
{
    var sw = new System.Diagnostics.Stopwatch();
    sw.Start();

    var timeString = "2012-02-18T12:00:01.8070000";
    var result = DateTime.Parse(
            timeString,
            CultureInfo.InvariantCulture,
            DateTimeStyles.RoundtripKind
            );

    sw.Stop();

    return sw.ElapsedTicks;
};

// ############### Run Tests ################

var format = "{0,-35}Average over {1:##,#} runs: {2,-10:G}";

var testResults = new[]
{ 
    new { Title = "Basic Parse", Result = runTest(parseBasic) },
    new { Title = "Parse, Null Culture", Result = runTest(parseNullCulture) },
    new { Title = "Parse, Current Culture", Result = runTest(parseCurrentCulture) },
    new { Title = "Parse, Invariant Culture", Result = runTest(parseInvariantCulture) },
    new { Title = "Parse Exact, Null Culture", Result = runTest(parseExactNullCulture) },
    new { Title = "Parse Exact, Current Culture", Result = runTest(parseExactCurrentCulture) },
    new { Title = "Parse Exact, Invariant Culture", Result = runTest(parseExactInvariantCulture)}
}
.OrderBy((test)=> test.Result)
.ToList();

testResults.ForEach((test)=>P(format, test.Title, runCount, test.Result));
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: