The Mathematica manual states

TimeConstrained takes account only of CPU time spent inside the main Mathematica kernel process; it does not include additional threads or processes.

However, my computation involves a parallel computation. As an example, consider the code

count = 0;
ProgressIndicator[Dynamic[count], {0, 500}]

f = Total[ParallelTable[count++; i^2, {i, 1, 500}]];

TimeConstrained[f, .1, "fail"]

(* 41791750 *)

How can I achieve that "fail" is returned. I can imagine that it could be complicated to compute the total CPU time of all sub-processes, but for my application it would be sufficient to consider the absolute amount of real-time that has passed as AbsoluteTiming[] does.

Any suggestions?


1 Answer 1


You should change

f = Total[ParallelTable[count++; i^2, {i, 1, 500}]];


f := (count = 0; Total[ParallelTable[count++; i^2, {i, 1, 500}]]);

Then, at least in Mathematica v.9, it works.

The fact is that f must be calculated only inside TimeConstrained and not before. The counter must be zeroed before any parallel evaluation


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.