可以Environment.TickCount用来计算时间跨度吗?
Environment.TickCount
int start = Environment.TickCount; // Do stuff int duration = Environment.TickCount - start; Console.WriteLine("That took " + duration " ms");
因为它TickCount是有符号的,并且将在25天后翻转(要花费50天才能全部击中32位,但是如果您想对数学有任何了解,就必须报废有符号的位),这似乎太冒险了,无法使用。
TickCount
我正在使用DateTime.Now。这是最好的方法吗?
DateTime.Now
DateTime start = DateTime.Now; // Do stuff TimeSpan duration = DateTime.Now - start; Console.WriteLine("That took " + duration.TotalMilliseconds + " ms");
使用秒表类。msdn上有一个不错的示例:http : //msdn.microsoft.com/zh- cn/library/system.diagnostics.stopwatch.aspx
Stopwatch stopWatch = Stopwatch.StartNew(); Thread.Sleep(10000); stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed;