Here is some simple code:
ClearSystemCache[];
d = 10^4;
mem1 = MemoryInUse[];
Sum[Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}];
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
which outputs
3764.59
I wonder why Mathematica takes so much memory for this calculation. Trying to nudge it in the desired direction, I replaced the line Sum[Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}];
by
s = 0; Do[s = s + Sqrt[1 + j^2] // N[#, d] &, {j, 1, d}]
but got an even bigger output,
3971.35
As I see it, to compute the sum using the Do[]
command, one only needs to keep in memory at most d digits of the current value of s, a few digits for the values of d=10^4 and j=1, ..., d, and however many digits needed to compute the current value of
Sqrt[1 + j^2] // N[#, d] &
, which seems to be no more than just 1.2d digits on an average (somewhat surprisingly to me); indeed,
ClearSystemCache[];
d = 10^4;
mem1 = MemoryInUse[];
Sqrt[1 + 1^2] // N[#, d] &;
Sqrt[1 + d^2] // N[#, d] &;
mem2 = MemoryInUse[];
(mem2 - mem1)/d // N
outputs
2.3144
half of which is less than 1.2.
So, it seems that the total memory needed for this calculation should be, at most, about d+1.2d=2.2d digits, whereas Mathematica seems to say it takes almost 4000d bytes of memory.
Where is my mistake? More importantly, is it possible to get the desired behavior from Mathematica (preferably without losing the speed), and if so, how? Thank you for your help.