DateTimeOffset.ParseExact .NET 8 performance improvements
Date Added (UTC):
15 Apr 2024 @ 19:45
Date Updated (UTC):15 Apr 2024 @ 19:45
.NET Version(s): Tag(s):
Added By:
.NET Developer and tech lead from Ireland!
Benchmark Results:
Benchmark Code:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using System.Globalization;
using System;
[HideColumns("Error", "StdDev", "Median", "RatioSD")]
[MemoryDiagnoser(displayGenColumns: false)]
[Config(typeof(Config))]
public class DateTimeParseExact
{
private const string Format = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
private readonly string _s = new DateTime(1955, 11, 5, 6, 0, 0, DateTimeKind.Utc).ToString(Format, CultureInfo.InvariantCulture);
[Benchmark]
public void ParseExact() => DateTimeOffset.ParseExact(_s, Format, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AssumeUniversal);
private class Config : ManualConfig
{
public Config()
{
AddJob(Job.Default.WithId(".NET 7").WithRuntime(CoreRuntime.Core70).AsBaseline());
AddJob(Job.Default.WithId(".NET 8").WithRuntime(CoreRuntime.Core80));
SummaryStyle =
SummaryStyle.Default.WithRatioStyle(RatioStyle.Percentage);
}
}
}
Powered by SharpLab
// .NET 7, .NET 8
public void ParseExact()
{
DateTimeOffset.ParseExact(_s, "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AssumeUniversal);
}
Powered by SharpLab
// .NET 7
.method public hidebysig
instance void ParseExact () cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor(int32, string) = (
01 00 1e 00 00 00 01 5f 00 00
)
// Method begins at RVA 0x208e
// Code size 25 (0x19)
.maxstack 8
// sequence point: (line 31, col 33) to (line 31, col 165) in _
IL_0000: ldarg.0
IL_0001: ldfld string DateTimeParseExact::_s
IL_0006: ldstr "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"
IL_000b: call class [System.Runtime]System.Globalization.CultureInfo [System.Runtime]System.Globalization.CultureInfo::get_InvariantCulture()
IL_0010: ldc.i4.s 68
IL_0012: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::ParseExact(string, string, class [System.Runtime]System.IFormatProvider, valuetype [System.Runtime]System.Globalization.DateTimeStyles)
IL_0017: pop
IL_0018: ret
}
// .NET 8
.method public hidebysig
instance void ParseExact () cil managed
{
.custom instance void [BenchmarkDotNet.Annotations]BenchmarkDotNet.Attributes.BenchmarkAttribute::.ctor(int32, string) = (
01 00 1e 00 00 00 01 5f 00 00
)
// Method begins at RVA 0x2050
// Code size 25 (0x19)
.maxstack 8
// sequence point: (line 31, col 33) to (line 31, col 165) in _
IL_0000: ldarg.0
IL_0001: ldfld string DateTimeParseExact::_s
IL_0006: ldstr "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"
IL_000b: call class [System.Runtime]System.Globalization.CultureInfo [System.Runtime]System.Globalization.CultureInfo::get_InvariantCulture()
IL_0010: ldc.i4.s 68
IL_0012: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::ParseExact(string, string, class [System.Runtime]System.IFormatProvider, valuetype [System.Runtime]System.Globalization.DateTimeStyles)
IL_0017: pop
IL_0018: ret
}
Powered by SharpLab
|
|
Benchmark Description:
### General Setup Overview
The benchmark setup provided is designed to measure the performance of parsing a date-time string into a `DateTimeOffset` object using the `ParseExact` method in different .NET runtime versions. The benchmark is configured using BenchmarkDotNet, a powerful library for benchmarking .NET code. Here's a breakdown of the general setup:
- **.NET Versions:** The benchmark is specifically comparing performance across two versions of the .NET runtime: .NET 7 and .NET 8. This is achieved by configuring two separate jobs, one for each runtime version, with .NET 7 set as the baseline for comparison.
- **Configuration:** The benchmark class is annotated with attributes that control the output and behavior of the benchmark. `HideColumns` hides specific columns in the output report for clarity, while `MemoryDiagnoser` is configured to not display garbage collection generation columns.
- **Custom Config:** A custom configuration class (`Config`) is defined within the benchmark class, specifying the jobs (runtime versions) to run and customizing the summary style to display ratio differences in percentage format.
### Benchmark Method: `ParseExact`
#### Purpose and Performance Aspect
The `ParseExact` method benchmark is designed to measure the performance of parsing a specific date-time string format into a `DateTimeOffset` object with specific parsing options. This operation is common in applications that need to interpret date-time data from text sources (like logs, external APIs, etc.) with known formats.
#### What It Measures
The benchmark measures:
- **Throughput:** How quickly the .NET runtime can parse the date-time string using the `ParseExact` method.
- **Allocation:** Although the `MemoryDiagnoser` is configured not to display garbage collection generation columns, it's still tracking the memory allocated by the operation. This can provide insights into the efficiency of the parsing operation in terms of memory usage.
#### Importance
Parsing date-time strings efficiently is crucial for performance-sensitive applications, especially those dealing with large volumes of data or requiring real-time processing. Poor performance or excessive memory allocation in these operations can lead to bottlenecks.
#### Expected Results or Insights
- **Performance Comparison:** By running this benchmark across different .NET versions, you can identify performance improvements or regressions in the `DateTimeOffset.ParseExact` method between .NET 7 and .NET 8.
- **Efficiency Insights:** The benchmark can reveal how memory-efficient the parsing operation is across the compared .NET versions. Even if throughput remains similar, improvements in memory allocation can lead to reduced garbage collection pressure and better overall performance.
In summary, this benchmark method provides valuable insights into the performance and efficiency of date-time parsing operations across different versions of the .NET runtime, helping developers make informed decisions about upgrading or optimizing their applications.