首页 > 代码库 > 统计操作耗时的类
统计操作耗时的类
//该类用于计算操作的耗时.
public class RunTime
{
DateTime start = DateTime.Now;
TimeSpan elapsed = new TimeSpan(0);
public override string ToString()
{
elapsed = (DateTime.Now - start);
if (elapsed.TotalMilliseconds > 600000.0)
{
//当总耗时超过10分钟时,返回格式: 时:分:秒.
return d(3600000) + ":" + d(600000) + d(60000) + ":" + d(10000) + d(1000);
}
else
{
//当总耗时小于10分钟时,返回格式:分:秒.毫秒
return d(60000) + ":" + d(10000) + d(1000) + "." + d(100) + d(10);
}
}
//传入一个位的毫秒的倍数级,然后返回在该位上的数字值,并将总耗时减去该位上的值.
protected internal virtual string d(long scale)
{
long report = (long)Math.Floor(elapsed.TotalMilliseconds / (double)scale);
long remaining = (long)Math.Floor(elapsed.TotalMilliseconds - (double)(report * scale));
elapsed = new TimeSpan(remaining * 10000); // 1ms = 10000ticks
return report.ToString();
}
}