5
$\begingroup$

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.

timing the integrals

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.

memoryusage

Do you think this is valid in general for any kind of argument?

$\endgroup$
2

1 Answer 1

9
$\begingroup$

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]]

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:

enter image description here

With //Normal on arguments1:

enter image description here

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}}]]

enter image description here

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

enter image description here

$\endgroup$
7
  • $\begingroup$ Thanks a lot @Feyre just for a matter of clarity would you kindly label your plots? $\endgroup$ Nov 24, 2016 at 10:02
  • $\begingroup$ To complete the answer you should also add results about MaxMemoryUsed. $\endgroup$ Nov 24, 2016 at 10:05
  • $\begingroup$ @MirkoAveta What is the code you used for that? $\endgroup$
    – Feyre
    Nov 24, 2016 at 10:10
  • $\begingroup$ I'll add the code in a moment. $\endgroup$ Nov 24, 2016 at 10:12
  • 1
    $\begingroup$ @MirkoAveta That should be it. $\endgroup$
    – Feyre
    Nov 24, 2016 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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