This repository contains benchmarks for various methods of extracting the year from a DateTime
string in .NET. The benchmarks are implemented using BenchmarkDotNet and target both .NET 6.0 and .NET 8.0 to compare performance across different runtime versions.
The following methods are benchmarked to extract the year from a DateTime
string:
- GetYearFromDate(): Parses the entire
DateTime
string usingDateTime.Parse
and returns the year. - GetYearFromSplit(): Splits the
DateTime
string by the '-' character and parses the year. - GetYearFromSubStr(): Uses
Substring
to extract the year part from theDateTime
string and parses it. - GetYearFromSpan(): Utilizes
ReadOnlySpan<char>
to slice theDateTime
string and manually converts the year part to an integer. - GetYearFromSpanWithManualConversion(): Uses
ReadOnlySpan<char>
to slice theDateTime
string and converts the year part usingint.Parse
.
Ensure you have .NET 6.0 and .NET 8.0 SDKs installed. You can check installed SDKs using:
dotnet --list-sdks
Clone the repository:
git clone https://github.com/omertayhan/BenchmarkDotNet.git
cd BenchmarkDotNet
Restore the dependencies:
dotnet restore
Run the benchmarks:
dotnet run -c Release
Sample Output BenchmarkDotNet will output the benchmark results, comparing the performance of each method across .NET 6.0 and .NET 8.0. The results will include metrics such as mean execution time, memory usage, and more.