Skip to content

DateTime.DaysInMonth minor performance tweaks #91103

@Charles113

Description

@Charles113

Hello, i did some performance testing and would propose changes to DateTime.DaysInMonth(int year, int month)

private static ReadOnlySpan<byte> DaysInMonth365 => new byte[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
DaysInMonth365[month - 1];

Change to:

private static readonly byte[] DaysInMonth365 = new byte[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
DaysInMonth365[month];

As the array is used as a lookup i would remove the -1 Operation, also i think DaysInMonth365 should be a field instead of a property.
I've noticed 5x Performance gains by doing lookups in a byte[] field instead of creating a new ReadOnlySpan every time through a property.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions