ETC =“预计完成时间”
我正在计算循环运行所需的时间,并向用户显示一些数字,告诉他/她整个过程大约需要多少时间。我觉得这是每个人偶尔都会做的常见事情,我想知道您是否有任何遵循的准则。
这是我目前正在使用的一个示例:
int itemsLeft; //This holds the number of items to run through.
double timeLeft;
TimeSpan TsTimeLeft;
list<double> avrage;
double milliseconds; //This holds the time each loop takes to complete, reset every loop.
//The background worker calls this event once for each item. The total number
//of items are in the hundreds for this particular application and every loop takes
//roughly one second.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//An item has been completed!
itemsLeft--;
avrage.Add(milliseconds);
//Get an avgrage time per item and multiply it with items left.
timeLeft = avrage.Sum() / avrage.Count * itemsLeft;
TsTimeLeft = TimeSpan.FromSeconds(timeLeft);
this.Text = String.Format("ETC: {0}:{1:D2}:{2:D2} ({3:N2}s/file)",
TsTimeLeft.Hours,
TsTimeLeft.Minutes,
TsTimeLeft.Seconds,
avrage.Sum() / avrage.Count);
//Only using the last 20-30 logs in the calculation to prevent an unnecessarily long List<>.
if (avrage.Count > 30)
avrage.RemoveRange(0, 10);
milliseconds = 0;
}
//this.profiler.Interval = 10;
private void profiler_Tick(object sender, EventArgs e)
{
milliseconds += 0.01;
}
由于我是一名刚开始职业生涯的程序员,我很想知道你在这种情况下会做什么。我主要关心的是我为每个循环计算和更新 UI,这是不好的做法吗?
当涉及到这样的估计时,有什么做/不做的事情吗?是否有任何首选方法,例如每秒更新一次、每十个日志更新一次、分别计算和更新 UI?此外,ETA/ETC 何时是一个好/坏的主意。