Consider these two types of creating matrices:
Table[Table[i*j,{i,1,10}],{j,1,10}]
Table[i*j,{i,1,10},{j,1,10}]
I want to know if there is any difference between these two in terms of memory efficiency.
N@(ByteCount[Table[Table[i*j, {i, 1, 10}], {j, 1, 10}]]/ByteCount[Table[i*j, {i, 1, 10}, {j, 1, 10}]])
If the size of the matrix is 10by10 both methods use the same amount of memory, for 100by100 the first method uses more memory, and for 1000by1000 case both use the same amount of memory.
Why aren't both method memory efficient equally?
Why the second method has advantage just over a limited range of matrix size?
The second part is comparing these two codes:
n = 100;
ByteCount[Table[i*j, {i, 1, n}, {j, 1, n}]]
(*250496*)
and
ByteCount[Table[i*j, {i, 1, 100}, {j, 1, 100}]]
(*80152*)
Why does setting the limits of the table by n
make it less memory efficient?
I use Mathematica 9.
Edit
If I make a table of matrices, there isn't significant difference in size between packed and unpacked arrays:
<< Developer`
id = IdentityMatrix[100];
a = Table[{id[[i]]}\[Transpose].{id[[i]]}, {i, 1, 100}];
b = ToPackedArray[Table[{id[[i]]}\[Transpose].{id[[i]]}, {i, 1, 100}]];
PackedArrayQ[a]
PackedArrayQ[b]
ByteCount[a]/(1.0*ByteCount[b])
Table[Table[i*j,{i,1,10},{j,1,10}]
$\endgroup$ – DavidC Jan 5 '15 at 15:51