Documentation
Mathematica
Built-in Functions
Advanced Documentation
Linear Algebra
Performance

Packed Arrays
One of the challenges of building Mathematica is to make sure that the system is general enough to support symbolic computation and fast enough so that numerical computation is efficient. These and other goals are described in the section Design Principles of Mathematica. One technique that Mathematica provides is to use specialized representations for important types of Mathematica expression. In the case of linear algebra this involves what is known as Packed Array Technology, and it was first introduced in Mathematica 4.0.
An example shows a vector of real numbers.
In[1]:=
Out[2]=
In[3]:=
Out[3]=
Now insert a symbolic quantity into the vector. Updating elements of vectors and matrices is discussed in the section Setting Pieces of a Matrix.
In[4]:=
Out[4]=
When the byte count is measured the vector is shown to use more memory.
In[5]:=
Out[5]=
In addition, a mathematical operation on the vector is much slower.
In[6]:=
Out[6]=
The first vector uses less memory and supports faster computations because it is built from a packed array. Packed arrays are a representation for rectangular tensors of machine integers, machine reals, and complex numbers with real and imaginary parts that are machine reals.

Packed array types.
There are a number of functions that are useful for working with packed arrays. Developer`PackedArrayQ tests if its argument is a packed array. This demonstrates that the result of the Table computation is a packed array.
In[7]:=
Out[8]=
If a real number is inserted into the array, the result is still packed.
In[9]:=
Out[10]=
If a computation is done with the packed array, the result is packed.
In[11]:=
Out[12]=
However, if a general symbolic quantity is inserted into the packed array, the result is not packed.
In[13]:=
Out[14]=
All the elements of the packed array must be of the same type. Therefore, if the integer 0 is inserted into a packed array of reals, the result is not packed.
In[15]:=
Out[17]=
Packed Array Functions
A number of functions for working with packed arrays are summarized below.

Packed array functions.
The function Developer`ToPackedArray can be used to make a packed array.
In[1]:=
Out[2]=
If the elements of the list are not all of the same type the result will not be a packed array.
In[3]:=
Out[4]=
By specifying a type of Real, the integers are coerced to reals and the result is packed.
In[5]:=
Out[6]=
This shows that pack is a packed array of three reals.
In[7]:=
Out[7]=
This does not make a packed array because the argument is not a rectangular tensor.
In[8]:=
Out[9]=
Packed Array Operations
The important detail of packed arrays is that they work exactly like lists except for the packed array specific functions described above. For example, SameQ on the packed and non-packed version of a list will return True.
In[1]:=
Out[2]=
The packed and non-packed versions will behave the same in the pattern matcher.
In[3]:=
Out[3]=
In[4]:=
Out[4]=
Many functions can work more efficiently with packed arrays compared with unpacked lists, but the results will be the same, whether a packed or an unpacked version is used. In order to maintain consistency, the system must, on occasion, unpack a packed array. Because this is a potential source of inefficiency, a message is available to let you know that something has become unpacked. You can enable this message with a system option.
This sets the system option and creates a packed array to use as an example.
In[5]:=
Out[7]=
Now if a real number is inserted into the packed array, it must be unpacked and a message is produced. This message can warn you that your code may not run as efficiently.
In[8]:=

It is usually a good idea to turn the message off again.
In[9]:=
Packed Array Summary
The main point about packed arrays is that Mathematica uses them automatically where appropriate. This is very much the case for the linear algebra functions, which naturally work with the same internal representation. Thus many functions return packed arrays even if the input is not packed. For example, the function Dot returns a packed array because internally it works with the packed version.
In[1]:=
Out[2]=
To make use of packed arrays, it is typically hard to exploit functions such as Developer`ToPackedArray. The main issue is to make sure that you have a uniformity of elements in your matrices; for example, that you are not mixing integers and reals.