[Date Index]
[Thread Index]
[Author Index]
Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg88177] Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)
*From*: m.r at inbox.ru
*Date*: Sun, 27 Apr 2008 04:59:01 -0400 (EDT)
*References*: <fupm2j$s4a$1@smc.vnet.net> <fus8g9$850$1@smc.vnet.net>
On Apr 25, 4:32 am, Alexey Popkov <pop... at gmail.com> wrote:
> On 24 =C1=D0=D2, 14:06, "W_Craig Carter" <ccar... at mit.edu> wrote:
>
>
>
> > (*let's use a do loop*)
> > Timing[
> > =9Aicount = 0;
> > =9ADo[icount = icount + i, {i, 1, 10000000, 1}];
> > =9Aicount
> > =9A]
>
> > (*this returns {10.2826, 50000005000000} on my machine.*)
> > (*10.28 being a measure of how long the computation took to run*)
>
> > (*lets try a table*)
> > Timing[
> > =9ATotal[Table[i, {i, 1, 10000000, 1}]]
> > =9A]
>
> > (*This returns {3.25516, 50000005000000} on my machine*)
>
> > W. Craig Carter
>
> Hello,
> Lets try the following:
>
> mmu = MaxMemoryUsed[];
> Timing[icount = 0;
> Do[icount = icount + i, {i, 1, 10000000, 1}];
> icount]
> MaxMemoryUsed[] - mmu
>
> (*This returns
> {25.657, 50000005000000}
> 1118080
> on my machine*)
>
> mmu = MaxMemoryUsed[];
> Timing[Total[Table[i, {i, 1, 10000000, 1}]]]
> MaxMemoryUsed[] - mmu
>
> (*This returns
> {7.651, 50000005000000}
> 479959784
> on my machine but takes much more time because of using PAGEFILE*)
>
> 479959784/1118080=429.271
> Constructing Table takes 429 times more memory than using Do[]!
The difference in timings is not surprising: in the example with Do
the expression icount = icount + i goes through the interpreter 10^7
times, while in the Table example Plus or Total is only interpreted
once and then sent to the low-level code, and additionally Table uses
autocompilation.
The reason for the memory usage difference is less obvious. Since the
result of Total is not a machine number (at least on 32-bit machines),
the table is being unpacked and then it takes up (20 bytes per
element)*10^7. Apparently a copy is created somewhere along the way,
which gives the 400Mb memory footprint.
I suppose one can imagine a world where Total doesn't unpack a packed
array and doesn't create a copy, but in this case you can avoid the
problem by using reals, since the result is a machine real:
In[1]:= Timing[Total@ Table[i, {i, 1, 10^7, 1.}]]
Out[1]= {1.062, 5.0000005*10^13}
In[2]:= MaxMemoryUsed[]
Out[2]= 86264744
8*10^7 is the size of one packed array of reals. Or you can use
Compile and avoid creating any arrays:
In[3]:= Timing[Compile[{}, Module[{n = 10^7, s = 0.},
Do[s += i, {i, 10^7}]; s]][]]
Out[3]= {0.609, 5.0000005*10^13}
Of course then you might get rounding problems if your result doesn't
fit in 53 mantissa bits.
Maxim Rytin
m.r at inbox.ru
Prev by Date:
**Re: Problems with NSolve**
Next by Date:
**Re: Re: Wolfram User Interface Research?**
Previous by thread:
**Re: Print[Plot] vs Print[text,Plot]? (*now Do and Table*)**
Next by thread:
**2*m^z - m^z = ?**
| |