Reputation: 5894
I have a simple problem that I'm having more trouble with than I should.
It's pretty straight forward: I have a time the process started _startTime
, and the current time. I also have the total number of records I need to process _records
, and the current record number _current
.
How can I get a simple linear prediction of the end time? I've ended up in a rabbit hole of differencing ticks and stuff, but I imagine there's some simple DateTime or TimeSpan trickery I could be using,
TimeSpan delta = DateTime.Now - _startTime;
float progress = (float)_records / (float)_current;
Upvotes: 0
Views: 146
Reputation: 15139
hmm really sounds much simpler than it is probably
TimeSpan timeTakenSoFar = DateTime.Now - _startTime;
double percentageDoneSoFar = (double)_current / (double)_records;
TimeSpan timeTakingInTotal = timeTakenSoFar / percentageDoneSoFar;
DateTime finishTime = _startTime.Add(timeTakingInTotal);
This should work.
Update:
According to MSDN you cannot divide TimeSpan, but in that case you can use ticks: (I don't have a C# compiler at hand to check if it's 100% syntactically correct)
TimeSpan timeTakenSoFar = DateTime.Now - _startTime;
double percentageDoneSoFar = (double)_current / (double)_records;
double timeTakingInTotalInTicks = timeTakenSoFar.Ticks / percentageDoneSoFar;
DateTime finishTime = _startTime.AddTicks((long)timeTakingInTotal);
Upvotes: 1
Reputation:
var restRecords = _records - _current;
var averageTimeForRecord = delta.TotalMilliseconds / _current;
var restTime = restRecords * averageTimeForRecord;
var finishDate = DateTime.Now.AddMilliseconds(restTime);
Maybe that is what you need?
Upvotes: 0
Reputation: 3710
Take a sample every few cycles, and determine how long they took from start time.
now.subtract(_startTtime)
Divide that by the _current pointer, and get the time per cycle.
Multiple the time/cycle with ((_records - _current)
<- cycles you have left to do)
The higher your "sample" resolution, the more accurate your result... but it is still just a prediction.
Upvotes: 1