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: [mg109012] Re: Estimating memory usage of expressions
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sat, 10 Apr 2010 06:53:49 -0400 (EDT)
  • References: <hpkgl2$81m$1@smc.vnet.net> <hpml87$9r3$1@smc.vnet.net>

Am 09.04.2010 09:33, schrieb Nicola Mingotti:
> The object "1" and the object "list containing only the element 1" are different
> in nature so it's expectable they heve different Mathematica representation
> 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 ;-)
> 

The key word is PackedArray (search the documentation for ToPackedArray
to learn more):

ListPlot[Table[
  {k, ByteCount[Table[RandomInteger[], {i, k}]]},
  {k, 300}
  ]]

Developer`PackedArrayQ[Table[RandomInteger[], {i, 249}]]

Developer`PackedArrayQ[Table[RandomInteger[], {i, 250}]]

ListPlot[Table[
  {k, ByteCount[Developer`ToPackedArray[Table[RandomInteger[], {i, k}]]]},
  {k, 300}
  ]]

and then:

Show[ListLinePlot[Table[
   {k, ByteCount[
     Developer`ToPackedArray[Table[RandomInteger[], {i, k}]]]},
   {k, 50}
   ], PlotStyle -> Green],
 ListLinePlot[Table[
   {k, ByteCount[
     Developer`FromPackedArray[Table[RandomInteger[], {i, k}]]]},
   {k, 50}
   ], PlotStyle -> Red]
 ]

hth,

albert


  • Prev by Date: Re: if using Mathematica to solve an algebraic problem
  • Next by Date: Re: Mathematica Programming
  • Previous by thread: Re: Estimating memory usage of expressions
  • Next by thread: Re: Estimating memory usage of expressions