# Time constrained loop unit

I have a particularly difficult loop, in that it consists of two variables looping and sowing the result if a criteria is met. Overall it works for me, but some certain values for my variables take extremely long to compute, whilst most others go through instantly.

I would thus like to apply a time constraint for every calculation in my loop, but instead of aborting, i'd like for the loop to just break and continue to the next iteration.

How would i do this if my code looks like so:

Reap[Do[Do[(g = i;
o = 1;
Kbt = 1;
a = j;
p := Solve[(s^2) + g*(s^a) + (o^2) == 0, s];
b = Abs[Re[s /. p[[1]]]];
ot = Abs[Im[s /. p[[1]]]];
fi = Pi + ArcTan[(-1)*(ot/b)];
v = 2 ot - ((g*a*Sin[(1 - a)*fi])/((b^2) + (ot^2))^((1 - a)/2));
u = -2 b + ((g*a*Cos[(1 - a)*fi])/((b^2) + (ot^2))^((1 - a)/2));
Om = ArcTan[u/v];
B[r_] := (a big function);
H[t_] := (a big function);
fm = FindMinValue[H[t], {t, 1, 10}]);
If[fm >= 0, Sow[{a , g}]; Break[]], {i, 0, 5, 0.01}], {j, 0.44,
0.98, 0.01}]]


This code gives me a load of points which i want to graph (the criteria for each point is that it is positive in the interval 1-10, which i determine with FindMinValue on said interval). However having it take days because of some points that could easily be replaced with the next iteration or previous one, is a tad tedious.

• Have you profiled which step exactly is taking that long? Blindly, you could try using NSolve or FindRoot instead of Solve. Also, loops may not be the best method to go about your problem, if expressed in Mathematica. Could you rewrite your algorithm as a Table and parallelize it, or use other vector (e.g. Map) approaches? – MarcoB Nov 14 '16 at 21:56
• How about TimeConstrained with CheckAbort? Example: Do[ CheckAbort[TimeConstrained[ Pause[Mod[i, 3]]; Print[i], 1.5], Null ], {i, 10} ] – Szabolcs Nov 14 '16 at 22:13