What sort of algorithms are used to compute the progress of a function call?
Obviously its easy to do if you have a loop, something like:
Action<Double> notifier = d => Console.WriteLine("Completed: " + d.ToString());
Double _interval = 0.05;
for (int i = 0; i < 1000; i++)
{
int rem = default(int);
Math.DivRem(i, _interval, out rem);
if (rem == 0)
{
double progress = (double)i / 1000;
notifier(progress);
}
}
But what about when you just have some generic delegate and you’d like to run it asynchronously but also notify another thread of its progress, and you can’t gaurantee that you can straightforwardly use a for-loop? Some other straightforward ways might be:
1) Time the function first (very bad for performance though if its a long running task)
2) Store past timings of the function in a log and use that (but this doesnt necessarily take into account extra variables – cpu, memory, etc. at the specific time of the task)
However do any more advanced algorithms exist, even if only to approximate the progress?
The
BackgroundWorkerclass has an update callback, but in answer to your question of a ‘generic algorithm’ for finding completion, Not really. The closest you can get is estimating based on Function Length (http://www.ndepend.com/) which will get you length in lines of code.