# TimeConstrained[] not taking full amount of time [closed]

I am using Mathematica to evaluate an integral within a given time constraint. Note that I have read in values of 'BigList' or BL from a file. If the file has a value for (l,m) of zero, then I evaluate the integral, otherwise, I read in the value of the integral from the file. The idea is to continually allow more and more time and 'knock out' the easiest integrals each time, instead of having the script run for hours before stopping when it would get to 'easier' integrals later.

My problem is that, for the integrals which evaluate to zero, the output happens far too quickly. The integrals will ONLY evaluate to zero if the time constraint is met, these integrals will never be zero on their own, so I know that if I get an output of zero, that it did not evaluate the integral in the time constraint allowed.

The output, however, prints (l) (m) 0 (where (l) and (m) are actual numerical values of l and m) almost within less than a second of each other. This tells me that it is not spending the full time (15 seconds in the example below) on the integral. Does Mathematica somehow know that it will take longer and give up or is something else going on? Shouldn't it work for 15 seconds before stopping and resulting in zero?

29 (* total time for each integral allowed *)
30 tt = 15;
31 (* loop through NeedList and assign values *)
32 (* Print["BLsize = ", BLsize]; *)
33 For[i=1, i<=BLsize, i++,
34   If[BL[[i]][[3]]==0,
35     l=BL[[i]][[1]];
36     m=BL[[i]][[2]];
37     (* Assign Jm to be Integrate[Abs[Sin[m*theta],{theta,0,2*pi}]] *)
38     If[m==0,Jm=2*Pi,Jm=4];
39     Ilm=TimeConstrained[Integrate[Abs[LegendreP[l,Abs[m],z]],{z,-1,1}],tt,0];
40     If[Ilm==0, BL[[i]]={l,m,0},
41       If[l==0, BL[[i]]={l,m,1/(Ilm*Jm)}, BL[[i]]={l,m,2/(Ilm*Jm)}];
42     ];
43     (*Print[FortranForm[N[BL[[i]][[3]]]]];*)
44     Print[BL[[i]][[1]], " ", BL[[i]][[2]], " ", FortranForm[N[BL[[i]][[3]]]]];
45   ]
46 ];


EDIT: here is a full working example ...

finp = OpenRead["dens_coefs"];
(* BigList *)
BL = ReadList[finp,{Number,Number,Number}]
Close[finp];
BLsize = Length[BL];

(* total time for each integral allowed *)
tt = 20;
(* loop through NeedList and assign values *)
For[i=1, i<=BLsize, i++,
If[BL[[i]][[3]]==0,
l=BL[[i]][[1]];
m=BL[[i]][[2]];
(* Assign Jm to be Integrate[Abs[Sin[m*theta],{theta,0,2*pi}]] *)
If[m==0,Jm=2*Pi,Jm=4];
Ilm=TimeConstrained[Integrate[Abs[LegendreP[l,Abs[m],z]],{z,-1,1}],tt,0];
If[Ilm==0, BL[[i]]={l,m,0},
If[l==0, BL[[i]]={l,m,1/(Ilm*Jm)}, BL[[i]]={l,m,2/(Ilm*Jm)}];
];
Print[BL[[i]][[1]], " ", BL[[i]][[2]], " ", FortranForm[N[BL[[i]][[3]]]]];
]
];


an example of an input file might be:

3 -3 0.02829421210522584
3 -2 0.06666666666666667
3 -1 0.5706901657772195
3 0 0.
3 1 0.5706901657772195
3 2 0.06666666666666667
3 3 0.02829421210522584
4 -4 0.004464285714285714
4 -3 0.011904761904761904
4 -2 0.1361783763047951
4 -1 0.
4 0 0.
4 1 0.
4 2 0.1361783763047951
4 3 0.011904761904761904
4 4 0.004464285714285714


the program should read these numbers in, figure out which ones are zero (in this case, {3,0}, {4,-1}, {4,0}, {4,1}) and attempt to produce the corresponding integrals. it should take no more than 20 seconds, but if it returns 'zero' then it should take AT LEAST 20 seconds (very close to 20 seconds, maybe a few clock cycles over).

for what it is worth, i am running this from a command line script on a linux machine. i have tried TimeConstrained arguments in a Mathematica notebook (though not the exact same thing) and it waits, as it should, in the notebook, but it is not waiting the full 20 seconds in the script.

EDIT(2): for what it's worth, this seems to work as intended when running it from a notebook interface. it is only when running the script from command line that it gives me problems. any idea why that would happen?

-

## migration rejected from stackoverflow.comApr 21 at 14:16

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

## closed as off-topic by Oleksandr R., bbgodfrey, ciao, Sjoerd C. de Vries, belisariusApr 21 at 14:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Oleksandr R., bbgodfrey, ciao, Sjoerd C. de Vries, belisarius
If this question can be reworded to fit the rules in the help center, please edit the question.

Could you post a complete working minimal example (without the need to imagine what is in your file)? Also, please remove the line numbers, so easing the copy and paste of your code into Mma. –  belisarius Jul 1 '12 at 19:43
I'm not really following your application. Simply stated do you want TimeConstrained to pause for until 20 seconds have elapsed, even if the calculation only takes e.g. one second? –  Mr.Wizard Jul 5 '12 at 13:15
no, the problem is that it returns '0' right away on some cases. the integral should never evaluate to zero until 20 seconds of computation have been done because '0' is the return value upon failure of TimeConstrained. (failure is relative - it returns zero if it takes the full amount of time). obviously, it is a problem if it returns zero in less than half a second when i have alotted 20 seconds for the calculation. –  Laurbert515 Jul 5 '12 at 14:48
Despite your lengthy post I'm afraid I just don't understand the question; the default return value upon failure is $Aborted, not 0. Why would you specify it to be 0 if that conflicts with the other parameters of your program? Why don't you simply test for $Aborted if you want to know if a calculation was, well, aborted? Surely there is some aspect of this I don't understand; I'd genuinely like to be helpful if I can be made to understand it. –  Mr.Wizard Jul 9 '12 at 0:26
i have the output set to zero if it does not work because i have this writing to a file. these calculations take a long time, so the 'first' run of the program, i allow, say 20 seconds. the ones that were able to be calculated in that time are written as they are, the others are zero. then i run it again with a time alotment of, say, a minute. this time, however, i only calculate the values which are read as 'zero' in the file, because i know the others are already calculated correctly. –  Laurbert515 Jul 9 '12 at 13:26