I found somewhat strange behaviour of nested integration timings. Consider the following code:
a1 := NIntegrate[x , {x, 0, 1}];
a2[x_] := NIntegrate[x, {x, 0, 1}];
NIntegrate[a1, {x, 0, 1}] // AbsoluteTiming
NIntegrate[a2[x], {x, 0, 1}] // AbsoluteTiming
The output is usually like this:
{0.0171206, 0.5}
{0.0103186, 0.5}
Normally the evaluation time of a2
is 1.5-2 times faster. The only difference is the definition of the inner integration function - with or without argument. The definition of a2
makes no sense for me, but its real execution speed always higher.. Considering numerous similar calculations with much more complicated functions one can save almost half of the time with this trick. Can someone please explain how it works and whether it will always produce correct results?
UDP: I made the code as simple as I could.
a1
anda2
are not dependent ont
, anda2
'sx
is a dummy variable. What gives? $\endgroup$a2
is supposed to work; surelyNIntegrate[5 fun[5], {5, -4, 4}, args]
fora2[5]
does not at all make sense. $\endgroup$NIntegrate
and with beneficial speed.. $\endgroup$