# Timing and MaxMemoryUsage of Integrate doesn't respect linear properties of Integrate

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?

There's a number of confounding things going on here: Firstly, there's the fact that rendering an output changes the time something takes to compute:

A fairer way of timing the two would be:

RepeatedTiming[Integrate[x^2 + x, {x, 0, 1}];, 2]
RepeatedTiming[Integrate[x^2, {x, 0, 1}] + Integrate[x, {x, 0, 1}];,
2]


{0.0015, Null}

{0.0019, Null}

With a slightly longer time to compute the separated integrals, as one might expect as there is an additional calculation.

Then there's the way in which arguments is presented,

arguments1[]


1+x+O[x]^2

Which is given (and integrated) as:

SeriesData[x, 0, {1, 1}, 0, 2, 1];

Fixing this by adding //Normal to the line, significantly impacts the results:

arguments1 = Table[Series[Exp[x], {x, 0, i}], {i, 1, 200}]//Normal;
arguments2 = Table[MonomialList@Normal@Series[Exp[x], {x, 0, i}], {i, 1, 200}];//Normal


Without //Normal: With //Normal on arguments1: With //Normal on both arguments1 and arguments2:

l1 = LinearModelFit[timings1, x, x] // Normal;
l2 = LinearModelFit[timings2, x, x] // Normal;
Show[ListPlot[{timings1, timings2}, PlotRange -> All],
Plot[{l1, l2}, {x, 0, 200},
PlotStyle -> {{Thick, Purple}, {Thick, Red}}]] As you can see, in this situation both grow about approximately linearly, and the integral calculated together is nominally faster as expected.

This also more or less fixes the Memory issue, though as always with such things there are anomalies • Thanks a lot @Feyre just for a matter of clarity would you kindly label your plots? – Mirko Aveta Nov 24 '16 at 10:02
• To complete the answer you should also add results about MaxMemoryUsed. – Mirko Aveta Nov 24 '16 at 10:05
• @MirkoAveta What is the code you used for that? – Feyre Nov 24 '16 at 10:10
• I'll add the code in a moment. – Mirko Aveta Nov 24 '16 at 10:12
• @MirkoAveta That should be it. – Feyre Nov 24 '16 at 10:20