using System.Diagnostics;
public static void Main()
TimeSpan timeSpan = TimeSpan.FromTicks(-1231231231231);
Console.WriteLine("Original timespan: " + timeSpan);
Stopwatch stopwatch1 = Stopwatch.StartNew();
long baseMilliseconds = timeSpan.Ticks / TimeSpan.TicksPerMillisecond;
long remainder1 = timeSpan.Ticks % TimeSpan.TicksPerMillisecond;
if (Math.Abs(remainder1) >= TimeSpan.TicksPerMillisecond / 2)
baseMilliseconds += Math.Sign(timeSpan.Ticks);
TimeSpan rounded1 = TimeSpan.FromTicks(baseMilliseconds * TimeSpan.TicksPerMillisecond);
Console.WriteLine("Integer rounding method time (in ms): " + stopwatch1.Elapsed.TotalMilliseconds);
Console.WriteLine("Rounded1 Result: " + rounded1);
Stopwatch stopwatch2 = Stopwatch.StartNew();
long remainder = timeSpan.Ticks % TimeSpan.TicksPerMillisecond;
adjustment = Math.Abs(remainder) < TimeSpan.TicksPerMillisecond / 2
: TimeSpan.TicksPerMillisecond - (remainder * Math.Sign(timeSpan.Ticks));
TimeSpan rounded2 = TimeSpan.FromTicks(timeSpan.Ticks + adjustment);
Console.WriteLine("Second rounding method time (in ms): " + stopwatch2.Elapsed.TotalMilliseconds);
Console.WriteLine("Rounded2 Result: " + rounded2);