MATLAB Answers

0

How to give a certain part of program only a specified amount of time to run?

Asked by Ketan Sarode on 24 Jul 2012
I want to know how can we break out of a function if takes too much time. In case of iterative functions, we can check the total runtime spent after each iteration (e.g. using tic; toc) and using if condition we can break out of the function. But how can we do it for non-iterative functions or when we don't want to alter the function itself?
In short, if function out = fun(in) takes more than 'x'sec, function fun should break.
After reading http://blogs.mathworks.com/videos/2008/05/05/advanced-matlab-timer-objects/ blog post, I tried following strategy using timer object:
function caller
h=@tfun;
try
a=timer('TimerFcn',h,'startdelay',5);
start(a)
funn;
delete(a);
catch
disp('Program is taking more than 5 sec')
end
function tfun(obj,event)
disp('Taking too much time')
error('Too long')
end
function funn()
pause(10)
disp('I ran')
end
Here 'funn' is the function which I want to run for maximum 5 sec. According to my logic, after 5 sec timer object 'a' would have given error 'Too long' and would have come to catch statements. But the actual output of the program is:
>> caller
Taking too much time
??? Error while evaluating TimerFcn for timer 'timer-1'
Too long
I ran
>>
Since the timer function and the caller program are running independently, error statement in timer function doesn't go to catch statements.
Please help me in solving this problem either using timer objects or any other way.
I hope, the problem is clear.

  0 Comments

Sign in to comment.

1 Answer

Answer by Jan
on 24 Jul 2012

It is a bad programm8ing practize to let a function reply different values depending on the run-time. Due to different processor speed, available RAM, influences of other threads e.g. of the operating system, the runtime can and will vary. A time-limit will therefore introduce a random element in your code, such that cannot be classified as reliable anymore.
Therefore I strongly suggest to avoid a time limit, when this is not implicitely a part of the problem, e.g. time-outs for internet or satellite connections.
However, if you really want and need such a time limitation, there is no way to implement it without modifying the code. Otherwise you'd have to "kill" the function, which means to leave it in an undefined status with e.g. open files or allocated memory.
A suggetsion:
function myLongTermFunction
starttime = cputime; % Possible overflow!
x = sin(rand(1e5, 1e5));
if cputime - starttime > 2
disp('!!! Timeout !!!');
return;
end
clear('x');
y = sin(rand(1e5, 1e5));
if cputime - starttime > 2
disp('!!! Timeout !!!');
return;
end
etc.
This is ugly. Such ugly that I still recommend not to use it.
CPUTIME ignores at least the time stolen by other threads, in opposite to etime and clock.

  1 Comment

I want to avoid modifying code since the problem I am facing is in case of MATLAB toolbox function (fmincon). In my program, this function is called multiple times with different guesses of input vector. For some guesses (which I know are very bad), the function takes too much time and practically hangs. In such cases I want to break out of the function and try next guess.
I don't want to modify fmincon function since it may affect its functionality in regular cases. Actually for different unstable guesses, it 'hangs' in different parts of the code, so it will be very difficult to consider all such cases for modification.

Sign in to comment.