Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Time Remaining

Tags:

c#

algorithm

What's a good algorithm for determining the remaining time for something to complete? I know how many total lines there are, and how many have completed already, how should I estimate the time remaining?

like image 867
Aaron Smith Avatar asked Jan 23 '09 15:01

Aaron Smith


People also ask

How do you calculate remaining time?

Approach: Since the total minutes in a 24 hour complete day is 24 * 60 = 1440 minutes. Calculate the time completed in minutes. Calculate the time remaining in the form of minutes as total minutes – time completed.


2 Answers

Why not?

(linesProcessed / TimeTaken) (timetaken / linesProcessed) * LinesLeft = TimeLeft

TimeLeft will then be expressed in whatever unit of time timeTaken is.

Edit:

Thanks for the comment you're right this should be:

(TimeTaken / linesProcessed) * linesLeft = timeLeft

so we have

(10 / 100) * 200 = 20 Seconds now 10 seconds go past
(20 / 100) * 200 = 40 Seconds left now 10 more seconds and we process 100 more lines
(30 / 200) * 100 = 15 Seconds and now we all see why the copy file dialog jumps from 3 hours to 30 minutes :-)

like image 148
JoshBerke Avatar answered Oct 10 '22 07:10

JoshBerke


I'm surprised no one has answered this question with code!

The simple way to calculate time, as answered by @JoshBerke, can be coded as follows:

DateTime startTime = DateTime.Now; for (int index = 0, count = lines.Count; index < count; index++) {     // Do the processing     ...      // Calculate the time remaining:     TimeSpan timeRemaining = TimeSpan.FromTicks(DateTime.Now.Subtract(startTime).Ticks * (count - (index+1)) / (index+1));      // Display the progress to the user     ... } 

This simple example works great for simple progress calculation.
However, for a more complicated task, there are many ways this calculation could be improved!

For example, when you're downloading a large file, the download speed could easily fluctuate. To calculate the most accurate "ETA", a good algorithm would be to only consider the past 10 seconds of progress. Check out ETACalculator.cs for an implementation of this algorithm!

ETACalculator.cs is from Progression -- an open source library that I wrote. It defines a very easy-to-use structure for all kinds of "progress calculation". It makes it easy to have nested steps that report different types of progress. If you're concerned about Perceived Performance (as @JoshBerke suggested), it will help you immensely.

like image 40
Scott Rippey Avatar answered Oct 10 '22 05:10

Scott Rippey