0

Will repeated use of DateTime.Today negatively impact performance? Is saving the value in a variable more efficient?

I'm writing a few functions that will calculate a new date, based on today's date. To accomplish this I use DateTime.Today relatively often. I wondered if it's more efficient to save the value in a variable and use the variable over DateTime.Today, or is there no difference?

    public static DateTime GetFirstDateInThePast()
    {
        var calculatedYear = DateTime.Today.Year;
        var calculatedMonth = DateTime.Today.Month < 7 ? 1 : 7;
        var day = 1;

        return new DateTime(calculatedYear, calculatedMonth, day);
    }

vs

        public static DateTime GetFirstDateInThePast()
    {
        var today = DateTime.Today;

        var calculatedYear = today.Year;
        var calculatedMonth = today.Month < 7 ? 1 : 7;
        var day = 1;

        return new DateTime(calculatedYear, calculatedMonth, day);
    }
MeanGreen
  • 3,098
  • 5
  • 37
  • 63
  • Well, without having any idea on the internal implementation it will of course have an impact, but it won´t be meassurable at all and you shouldn´t care too much on it. Reading a property surely needs some time, no matter on the actual type. – MakePeaceGreatAgain Mar 08 '17 at 12:55
  • 2nd option is better. Low chance, but what if Day ticks over to a new month mid method. Your data get skewed –  Mar 08 '17 at 12:56
  • In addition to what I mentioned before: the performance-implication should be insiginifact. However the actual question is: does this code what you want? For this also consider xanatos´ answer. – MakePeaceGreatAgain Mar 08 '17 at 13:00
  • Sigh, another duplicate question, even though I did search with Google and the SO internal search. This older question did not show up sadly. Are my search skills lacking? – MeanGreen Mar 08 '17 at 13:18

1 Answers1

5

The first one is morally wrong... What happens if your code is running around 31 December 2017, 23.59.59? Perhaps the Year will be 2017 and the Month will be 1 :-) (the probability of this happening is very very very low... Probably it is more probable that a cosmic ray will make your PC crash, for this reason I consider it to be only morally wrong, and not really really wrong)

The second one is correct.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Good shout. Although, I wonder, would the IL be optimized against this? (i.e would it be optimized to a local var as in the second example) – Dave Becker Mar 08 '17 at 13:08
  • @dave Why should it optimize out a call to an external OS API (the one that gets the current time and that is used by `DateTime.Today`)? How should the compiler know what that API is doing? – xanatos Mar 08 '17 at 13:09
  • 1
    I'm not an expert (evidently) on .NET optimisation (hence why I "wondered") but was just curious if two calls one after another to a static property would be "converted" to a single call and stored in a local var, But now that I've said that our loud I guess it doesn't make much sense at all (especially given that you might actually want two different datetimes very close together). Ahem.....pretend I never asked....... – Dave Becker Mar 08 '17 at 13:19
  • @DaveBecker Assuming that something will be optimized out, that something will be cached because "it would be a good idea", is sadly wrong :-) As is assuming that something that always change will remain motionless just because it would be useful for you... Like doing a photo to a running person :-) – xanatos Mar 08 '17 at 13:27
  • yeah I realised it was pretty stupid, unfortunately, too late :-) `` – Dave Becker Mar 08 '17 at 13:31