I've got some timers that measure the time to execute code.
DateTime startTimeFunctionTotal = DateTime.Now;
for (int i = 0; i < array.Count; i++) {
DateTime startTimeFunction = DateTime.Now;
//some code here
DateTime stopTimeFunction = DateTime.Now;
TimeSpan durationTimeFunction = stopTimeFunction - startTimeFunction ;
}
DateTime stopTimeFunctionTotal = DateTime.Now;
TimeSpan durationTimeFunctionTotal = stopTimeFunctionTotal - startTimeFunctionTotal ;
It's allowed (even better) if the predicted time changes according to more data (every loop there is more data so prediction should be more accurate).
I would like to give user a predicted time of finish (both time like 15 minutes, and 10:56).
Use the continue Statement to Exit a Function in C# The continue statement skips the execution of a block of code when a certain condition is true. Unlike the break statement, the continue statement transfers the control to the beginning of the loop. Below is an example of code using a foreach method.
Assuming each action in your for statement takes roughly the same amount of time:
List<TimeSpan> timeSpans = new List<TimeSpan>();
for(int i = 0; i < array.Count; i++)
{
Stopwatch watch = Stopwatch.StartNew();
//do stuff
timeSpans.Add(watch.Elapsed);
long ticksLeft = timeSpans.Sum(ts => ts.Ticks) * (array.Count - i) / timeSpans.Count;
Console.WriteLine("Left: " + new TimeSpan(ticksLeft));
Console.WriteLine("Finished in: " +
DateTime.Now.AddTicks(ticksLeft));
}
To go with the median use:
long ticksLeft = timeSpans.OrderBy(ts => ts.Ticks)
.ElementAt(timeSpans.Count / 2).Ticks * (array.Count - i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With