MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Estimating memory usage of expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109002] Re: Estimating memory usage of expressions
  • From: Mike Bryniarski <melondisco at gmail.com>
  • Date: Sat, 10 Apr 2010 06:51:52 -0400 (EDT)
  • References: <hpkgl2$81m$1@smc.vnet.net> <hpml87$9r3$1@smc.vnet.net>

In general ByteCount is accurate.  If your array is large enough, as
long as it is not ragged and all entries are either machine size
floating points or integers (maybe complex as well? I don't see why
they would not be supported) Mathematica uses packed arrays. You can
use Developer`ToPackedArray[mat] to force a compatable matrix into a
packed array.
A packed array is WRI jargon for a more efficient list that only takes
up slightly more memory than its contents wheras the standard
Mathematica lists probably store the data type for each individual
element of the array as well as a pointer.
In this example you can see that the packed array version of a matrix
takes up much less space:

packed = ConstantArray[1, {1000, 1000}];
unpacked = Developer`FromPackedArray[packed];
ByteCount /@ {packed, unpacked}

the packed array uses about 4 bytes per entry whereas the unpacked
array needs about 20
-mike


On Apr 9, 3:33 am, Nicola Mingotti <nico020... at gmail.com> wrote:
> The object "1" and the object "list containing only the element 1" are di=
fferent
> in nature so it's expectable they heve different Mathematica representati=
on
> and different Memory usage.
>
> I don't know the C representation for a Mathematica List or a Mathematica
> Integer, i don't even know if they are public domain.
>
> Anyway, if you do
>
> ByteCount[{1}]      => 56
> ByteCount[{1,2}]    => 80
> ByteCount[{1,2,3}]   => 104
> ByteCount[{1,2,3,4}]   => 128
>
> you see the difference is always 24 bytes.
>
> It tried to do a little experiment, the list data structure changes
> when you add some elements :
>
> r = Table[RandomInteger[], {i, 100}];
> (ByteCount[r] - 32) / 24    => 100
>
> r = Table[RandomInteger[], {i, 200}];
> (ByteCount[r] - 32) / 24    => 200
>
> r = Table[RandomInteger[], {i, 250}];
> (ByteCount[r] - 32) /24     => 91/2       !!!!
>
> In the last result i would expect naively a result 250,
> 91/2 tells me that things underwood are more sophisticated.
>
> Hope somebody can tell you more ;-)
>
> bye
>
> n.
>
> On 2010-04-08 14:02:42 +0200, Jim Lambaugh said:
>
>
>
> > Hi
>
> > Is there a way to estimate how many bytes the different expressions in
> > Mathematica take from the memory? For example I wish to know how many
> > bytes a NxN matrix with real entries is, the list of its eigenvectors,
> > eigenvalues etc.
>
> > I know of the command "ByteCount", but the results I get are not
> > consistent, i.e. a list of 1 integers does not take up the same amount
> > of memory as one single integer etc. So: Is there a way to estimate
> > these things? I'm running on a 32-bit system (if that makes a
> > difference).



  • Prev by Date: Re: if using Mathematica to solve an algebraic problem
  • Next by Date: Re: Surprising behaviour with nested pure functions and
  • Previous by thread: Re: Estimating memory usage of expressions
  • Next by thread: Bug in PadLeft/PadRight