DateTime.Now.Ticks or Utility.GetMachineTime().Ticks

Hi,

which will be fastes and create less garbage?

DateTime.Now.Ticks or Utility.GetMachineTime().Ticks

Good question.

Both return numbers which are not objects, so GC should not be an issue.

As to speed, write a short program to do each 100,000 and measure the elapsed time.

If we look inside DateTime.Now it looks like this, there is objects that are created internal in the method which will generate garbage.


public static DateTime Now
{
	get
	{
		DateTime utcNow = DateTime.UtcNow;
		bool flag = false;
		TimeSpan dateTimeNowUtcOffsetFromUtc = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utcNow, out flag);
		long ticks = dateTimeNowUtcOffsetFromUtc.Ticks;
		long num = utcNow.Ticks + ticks;
		if (num > 3155378975999999999L)
		{
			return new DateTime(3155378975999999999L, DateTimeKind.Local);
		}
		if (num < (long)0)
		{
			return new DateTime((long)0, DateTimeKind.Local);
		}
		return new DateTime(num, DateTimeKind.Local, flag);
	}
}