﻿﻿﻿ Calculate Time Remaining
﻿

## ﻿Calculate Time Remaining

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?

Quick and Simple Hash Code Combinations

## 1:

How does Java implement hash tables?
Why not? .
Algorithm Question On Finding All Valid Words In Dictionary
`(linesProcessed / TimeTaken)` `(timetaken / linesProcessed) * LinesLeft = TimeLeft`.
What is the best way to go about writing a simple x86 assembler?
`TimeLeft` will then be expressed in whatever unit of time `timeTaken` is..
Efficient Out-Of-Core Sorting

# Edit:

Thanks for the comment you're right this should be:.
List implemented using an inorder binary tree
`(TimeTaken / linesProcessed) * linesLeft = timeLeft`.
Transforming multiple iterator elements
so we have.
How to get the greatest difference among a list of ordered numbers in PHP?
`(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 :-) .

## 2:

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.

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.

## 3:

Make sure to manage perceived performance..
Although all the progress bars took exactly the same amount of time in the test, two characteristics made users think the process was faster, even if it wasn't:.
1. progress bars that moved smoothly towards completion
2. progress bars that sped up towards the end

## 4:

You could create an extension method on the Stopwatch class to get functionality that would get an estimated remaining time span.

.
``static class StopWatchUtils {     /// <summary>     /// Gets estimated time on compleation.      /// </summary>     /// <param name="sw"></param>     /// <param name="counter"></param>     /// <param name="counterGoal"></param>     /// <returns></returns>     public static TimeSpan GetEta(this Stopwatch sw, int counter, int counterGoal)     {         /* this is based off of:          * (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 :-)          *           * pulled from http://stackoverflow.com/questions/473355/calculate-time-remaining/473369#473369          */         if (counter == 0) return TimeSpan.Zero;         float elapsedMin = ((float)sw.ElapsedMilliseconds / 1000) / 60;         float minLeft = (elapsedMin / counter) * (counterGoal - counter); //see comment a         TimeSpan ret = TimeSpan.FromMinutes(minLeft);         return ret;     } } ``
Example:.
``int y = 500; Stopwatch sw = new Stopwatch(); sw.Start(); for(int x = 0 ; x < y ; x++ ) {     //do something     Console.WriteLine("{0} time remaining",sw.GetEta(x,y).ToString()); } ``
Hopefully it will be of some use to somebody..
EDIT: It should be noted this is most accurate when each loop takes the same amount of time.

Edit 2: Instead of subclassing I created an extension method.

.

## 5:

I made this and it works quite good, feel free to change the method signature according to your variable types or also to the return type, probably you would like to get the TimeSpan object or just the seconds...

.
``    /// <summary>     /// Calculates the eta.     /// </summary>     /// <param name="processStarted">When the process started</param>     /// <param name="totalElements">How many items are being processed</param>     /// <param name="processedElements">How many items are done</param>     /// <returns>A string representing the time left</returns>     private string CalculateEta(DateTime processStarted, int totalElements, int processedElements)     {         int itemsPerSecond = processedElements / (int)(processStarted - DateTime.Now).TotalSeconds;         int secondsRemaining = (totalElements - processedElements) / itemsPerSecond;          return new TimeSpan(0, 0, secondsRemaining).ToString();     } ``
You will require to initialize a `DateTime` variable when the processing starts and send it to the method on each iteration.. Do not forget that probably your window will be locked if the process is quite long, so when you place the return value into a control, don't forget to use the `.Refresh()` method of it.. If you are using threads then you can attempt to set the text using the `Invoke(Action)` method, would be easier to use this extension method to archieve it easily.. If you use a console application, then you should not have problems displaying the output line by line.. Hope it helps someone..

## 6:

It depends greatly on what the "something" is.

If you can assume that the amount of time to process each line is similar, you can do a simple calculation:.
``TimePerLine = Elapsed / LinesProcessed TotalTime = TimePerLine * TotalLines TimeRemaining = TotalTime - LinesRemaining * TimePerLine ``

## 7:

Generally, you know three things at any point in time while processing:.
1. How many units/chunks/items have been processed up to that point in time (A).
2. How long it has taken to process those items (B).
3. The number of remaining items (C).
Given those items, the estimate (unless the time to process an item is constant) of the remaining time will be. B * C / A.

## 8:

there is no standard algorithm i know of, my sugestion would be:.
• Create a variable to save the %
• Calculate the complexity of the task you wish to track(or an estimative of it)
• Put increments to the % from time to time as you would see fit given the complexity.
You probably seen programs where the load bar runs much faster in one point than in another.

Well that's pretty much because this is how they do it.

(though they probably just put increments at regular intervals in the main wrapper).

## 9:

Where `time\$("ms")` represents the current time in milliseconds since 00:00:00.00, and `lof` represents the total lines to process, and `x` represents the current line:.
``if Ln>0 then     Tn=Tn+time\$("ms")-Ln   'grand total of all laps     Rn=Tn*(lof-x)/x^2      'estimated time remaining in seconds end if Ln=time\$("ms")             'start lap time (current time) ``

## 10:

That really depends on what is being done...

lines are not enough unless each individual line takes the same amount of time.. The best way (if your lines are not similar) would probably be to look at logical sections of the code find out how long each section takes on average, then use those average timings to estimate progress..

## 11:

If you know the percentage completed, and you can simply assume that the time scales linearly, something like . timeLeft = timeSoFar * (1/Percentage). might work..

## 12:

I already knew the percentage complete & time elapsed, so this helped me:. TimeElapsed * ((100 - %complete) / %complete) = TimeRemaining. I then updated this value every time %complete changed, giving me a constant varying ETA..

## 13:

There is 2 ways of showing time.
1. Time elapsed and Time Remaining overall: so elapsed will increase but remaining will be likely stable total time needed (if per second is stable).
2. Time elapsed and Time Left:
so Time Left = Total Needed - Elapsed.
My idea/formula is more likely like this:. Processed - updated from running thread from 0 to Total . I have timer with 1000ms interval that calculates processed per second:.
``processedPerSecond = Processed - lastTickProcessed; lastTickProcessed = Processed;  //store state from past call ``
processedPerSecond and lastTickProcessed are global variables out of timer method. Now if we would like to get how many seconds is required to complete the processing (in ideal constant assumption) totalSecondsNeeded = TotalLines / PerSecond. but we want to show case 2.

TimeLeft so TimeLeftSeconds = (TotalLines - Processed) / PerSecond.
``TimeSpan remaining = new TimeSpan(0, 0, (transactions.Count - Processed) / processedPerSecond); labelTimeRemaining.Text = remaining.ToString(@"hh\:mm\:ss"); ``
Of course TimeLeftSeconds will "jump" if PerSecond jumps, so if past PerSecond was 10 then 30 then back to 10, the user will see it.. There is a way to calculate average, but this may not show real time left if process speeds up at the end.
``int perSecond = (int)Math.Ceiling((processed / (decimal)timeElapsed.TotalSeconds));  //average not in past second ``
So it may be the choice for a developer to "pick" a method that will be most accurate based on prediction of how "jumpy" the processing is. We could also calculate and save each PerSecond, then take last 10 second and made average, but in this case user will have to wait 10 seconds to see first calculation or we could show time left starting from first per second and then progressively average summing up to 10 last PerSecond. I hope my "jumpy" thoughts will help someone to build something satisfying.

85 out of 100 based on 65 user ratings 865 reviews