基于cputime的MATLAB条件代码执行 [英] MATLAB conditional code execution based on cputime
问题描述
我有一个MATLAB代码,对于某些输入情况,它需要无限的时间来执行.我想从代码的执行中排除那些使我的MATLAB函数执行超过X分钟时间的输入值,因为MATLAB意识到执行时间太长.您知道这样做的任何方法吗?如果它是用C编写的,那么我可以通过并发来做到这一点,但是我对MATLAB中的并发性一无所知.
I have a MATLAB code which for some of the input cases takes an infinite time to execute. I would like to exluce from the execution of my code those input values which make my MATLAB function to take more than X minutes to execute, as MATLAB realises the execution is taking too long. Do you know any way of doing this? I could do this by concurrecy if it was written in C but I know nothing obut concurrency in MATLAB.
实际上,当我执行Control-C(我在Windows中)以中断这种无限执行时,代码实际上似乎正确完成了.
In fact, when I do Control-C (I am in windows) to interrupt such an infinite execution, the code actually appears to finish correctly.
非常感谢您.
推荐答案
如果您可以控制循环,则可以使用tic和toc来查找所需时间,如果时间太长,则停止:
If you're running in a loop that you have some control over, you can use tic and toc to find out how long you've been going for, and stop if that was too long:
start = tic;
for i=1:1000000
if (mod(i,1000)==0)
if (toc(start) > 2) % here, 2 seconds
error('Took toooo loooong');
end
end
end
这篇关于基于cputime的MATLAB条件代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!