MathGroup Archive 2007

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

Search the Archive

Creation of temporary copies of arrays when using x[[a]]=b

Please consider this input, which creates a large array of real numbers, and
then changes one element of that array, and tracks the memory used by this

$HistoryLength = 0;
Print[{MemoryInUse[], MaxMemoryUsed[]}];
 x = RandomReal[1, {1000000}];
 Print[{MemoryInUse[], MaxMemoryUsed[]}];
 x[[2345]] = 5.6;
 Print[{MemoryInUse[], MaxMemoryUsed[]}];
Print[{MemoryInUse[], MaxMemoryUsed[]}];

Here is the output (on my machine):


The second line of output shows that the code "x = RandomReal[1,
{1000000}];" used 8MB of memory, as expected for 1000000 real numbers.

The third line of output shows that the code x[[2345]]=5.6 uses *another*
8MB of memory, temporarily. Presumably this is because x[[a]]=b is
equivalent to x=ReplacePart[x,a->b], which would result in the temporary
creation of the 8MB object ReplacePart[x,a->b].

Question 1. Can anyone confirm that these are equivalent?

Because of this, an algorithm that relies on repeatedly changing single
elements of a large list by using x[[a]]=b is very slow.

Question 2. What is the simplest way to have lists for which the
reassignment of a single element is efficient? Is an external package
necessary, or can the built-in Mathematica functions be used for this

I note that if you take the input and enclose it in Compile[{},...][], then
then output becomes:


No temporary 8MB array is created in this case. However, I can only make
this work when x is defined *inside* the compiled function. If I move the
definition of x outside Compile[], then temporary objects are created again.

Question 3. Is there a more comprehensive set of documentation for Compile
than that found in the Mathematica documentation centre? I would like to
know the answer to questions like: How does Compile decide what assumptions
to make about the types of expressions found in the code to be compiled?

  • Prev by Date: Re: Problem using PrincipalComponents to rotate 3D model data
  • Next by Date: Re: StringCases kills Kernel
  • Previous by thread: Re: double integral of long expression in Mathematica
  • Next by thread: Formula simplify for Solve