我正在做一个长循环下载数千个文件。我想显示剩余的估计时间,因为它可能需要几小时。然而,用我写的,我得到的平均数为毫秒。如何将平均下载时间从毫秒转换为TDateTime
?如何将毫秒转换为TDateTime?
看到我设置Label1.Caption
:
procedure DoWork;
const
AVG_BASE = 20; //recent files to record for average, could be tweaked
var
Avg: TStringList; //for calculating average
X, Y: Integer; //loop iterators
TS, TE: DWORD; //tick counts
A: Integer; //for calculating average
begin
Avg:= TStringList.Create;
try
for X:= 0 to FilesToDownload.Count - 1 do begin //iterate through downloads
if FStopDownload then Break; //for cancelling
if Avg.Count >= AVG_BASE then //if list count is 20
Avg.Delete(0); //remove the oldest average
TS:= GetTickCount; //get time started
try
DownloadTheFile(X); //actual file download process
finally
TE:= GetTickCount - TS; //get time elapsed
end;
Avg.Add(IntToStr(TE)); //add download time to average list
A:= 0; //reset average to 0
for Y:= 0 to Avg.Count - 1 do //iterate through average list
A:= A + StrToIntDef(Avg[Y], 0); //add to total download time
A:= A div Avg.Count; //divide count to get average download time
Label1.Caption:= IntToStr(A); //<-- How to convert to TDateTime?
end;
finally
Avg.Free;
end;
end;
PS - 我愿意计算在过去的20(或AVG_BASE)下载的平均速度不同的方式,因为我相信我的字符串列表解决方案不是最好的。我不想根据所有下载来计算它,因为速度可能会随着时间而改变。因此,我只是检查最后20个。
+1 TStopWatch。请注意,在某些计算机上,我发现TStopWatch的底层Win32 API函数不稳定并且无法正常工作(Windows XP,某些采用ICH4/ICH5时代芯片组的Intel Pentium 4芯片组)。在现代硬件上(Core2Duo和更高版本)不再是问题,但是......我花了很多时间测量毫秒(准确地做到这一点),Windows API让我非常难受。 (我并不是说我想要实时,只需要一个毫秒级的计数器,它不会在我身上停滞不前) - TStopWatch比GetTickCount准确得多! –
+1确实,现在如果只有我可以考虑文件大小......尽管.......完全不同的故事........ –