Let's say that I have a function f[i]
that takes quite a while to evaluate (evaluation time is different for different i) and I'd like to have as many values of f[i]
as possible, for i = 1, 2, 3, ...
, but in a specified amount of time (say, one minute) I want to stop evaluation and return values for i = 1, ... iFeasible
(there is no way to calculate iFeasible
before full computation). Basically, what I want to achieve is:
TimedConstainedTable[expr, {i, imin, imax}, maxTime]
To simplify:
"If evaluating Table[expr, {i, imin, imax}]
takes more time than maxTime
, detect it, and return Table[expr, {i, imin, iFeasible}]
(but do not evaluate values of expr for i = imin, ..., iFeasible
again; extract them somehow from interrupted evaluation)."
It is super easy to implement:
list = {}; TimeConstrained[Do[AppendTo[list, f[i]], {i, 10^6}], 60, list]
but also very inefficient. Any improvement would be great.