BEFORE READING IT IS BEST YOU KNOW THAT FEYRE's ANSWER PROVES MY STATEMENT TO BE FALSE
I have the following Integrals:
Timing[Integrate[x^2 + x, {x, 0, 1}]]
Timing[Integrate[x^2, {x, 0, 1}] + Integrate[x, {x, 0, 1}]]
In the first case I have:
{0.009273, 5/6}
In the second:
{0.00944, 5/6}
This means that somehow Mathematica optimizes evaluations in the first case. How does it manage to do this??? Secondly how much is this related to memory usage? Does Mathematica use more memory in the first case or in the second?
Update on Memory usage
MaxMemoryUsed[Integrate[x^2 + x, {x, 0, 1}]]
MaxMemoryUsed[Integrate[x^2, {x, 0, 1}] + Integrate[x, {x, 0, 1}]]
Outputs:
{0.006281, 5/6}
{0.008078, 5/6}
This means that not only it optimizes time but also max memory usage. Why?
Is it always convenient to write a huge integral instead of more integrals??? This is the question.
EDIT: ABSOLUTE TIMING
Absolute timing proves the opposite:
AbsoluteTiming[Integrate[x^2 + x, {x, 0, 1}]]
AbsoluteTiming[Integrate[x^2, {x, 0, 1}] + Integrate[x, {x, 0, 1}]]
Returns:
{0.199372, 5/6}
{0.006759, 5/6}
Still not what I expected. And this would mean that one would need to find a compromise between time expenditure and memory expenditure?
UPDATE
I've tried to see what happens when increasing the number of arguments to sum.
arguments1 = Table[Series[Exp[x], {x, 0, i}], {i, 1, 200}];
list1 = Table[
AbsoluteTiming@Integrate[arguments1[[i]], {x, 0, 1}], {i, 1, 200}];
arguments2 =
Table[MonomialList@Normal@Series[Exp[x], {x, 0, i}], {i, 1, 200}];
list2 = Table[
AbsoluteTiming@
Sum[Integrate[arguments2[[j]][[i]], {x, 0, 1}], {i, 1,
Length[arguments2[[j]]]}], {j, 1, Length[arguments2]}];
timings1 = Table[list1[[i, 1]], {i, 1, 200}];
timings2 = Table[list2[[i, 1]], {i, 1, 200}];
This is the result.
As for memory usage. EDIT
arguments1 = Table[Series[Exp[x], {x, 0, i}], {i, 1, 200}];
list1 = Table[
MaxMemoryUsed@Integrate[arguments1[[i]], {x, 0, 1}], {i, 1, 200}];
arguments2 =
Table[MonomialList@Normal@Series[Exp[x], {x, 0, i}], {i, 1, 200}];
list2 = Table[
MaxMemoryUsed@
Sum[Integrate[arguments2[[j]][[i]], {x, 0, 1}], {i, 1,
Length[arguments2[[j]]]}], {j, 1, Length[arguments2]}];
I have an even more terribly nonlinear behavior of the huge integral if compared to the sum of integrals.
Do you think this is valid in general for any kind of argument?
AbsoluteTiming[]
give? reference.wolfram.com/language/ref/AbsoluteTiming.html $\endgroup$