I was reading a useful post at WRI blog on improving speed of code, and I need help in understanding this one.
Compare these speeds
Timing[
tbl = Table[i + j, {i, 1, 1000}, {j, 1, 1000}];
]
{0.031, Null}
and
Timing[
a = 1000;
tbl = Table[i + j, {i, 1, a}, {j, 1, a}];
]
{0.422, Null}
So it is much faster when putting the actual value for the limit inside the table itself vs outside. The explanation for this, which I am sure it is correct, but I need help in understanding, is that Table
is compiled if its limit are numeric vs. not, this is because its Attributes is HoldAll
.
But my question is: How would the above actually work, because the limits to Table
must, at one point, become numeric anyway? I can't write
Clear[a]
tbl = Table[i + j, {i, 1, a}, {j, 1, a}]
The above gives an error.
So, for me, writing a=1000
outside Table
vs. inside, should have made no difference, since without a
having a numerical value, Table[]
can't do anything. So the replacing of a
by the number 1000 must occur at one point of time by evaluator before Table[]
can do anything useful, would it not?
In other words, what Table
should see, eventually, is {i, 1, 1000}, {j, 1, 1000}
in both cases.
So, the way I thought this would happen is this:
- Evaluator replaces
a
by 1000 in the arguments of table - Evaluator calls
Table
with the result, which is now all numeric. - Table Compiles, and runs faster now.
But what seems to happen is something else. (due to HoldAll
?)
- Table takes its arguments, as is. Since it has HoldAll, so it sees
a
and not 1000. - It does not call Compile since its arguments are not all numbers.
- It now generate a table with the
a
limit, Evaluator evaluatesa
to 1000 - Table is generated now all limits are numeric, but slower now since code is not compiled.
Question is: Does the above sort of what happens? Could someone explain the steps that would have happened to explain this difference in timing?
Also, how would one insure that Table is Compiled in both cases in the above example, even if one uses a variable for the limit? It is not always possible to hardcode the numbers for the table limits, but one must sometime use a variables for these. Should one explicitly use the Compile
command? (I do not use Compile
directly, since I assumed it is done automatically when needed).
edit(1)
In answer to post by Mike below on finding no difference in timing when using a call.
ClearAll[tblFunc];
Timing[a = 1000;
tblFunc[a_] := Table[i + j, {i, 1, a}, {j, 1, a}];
Developer`PackedArrayQ[tblFunc[a]]
]
gives
{0.031, True}
But that is because a
is now the number 1000
INSIDE the function, once it is called. Since M passes things by VALUE.
If we force the call to be by reference, so that a
is left unevaluated, then we get
ClearAll[tblFunc];
Timing[a = 1000;
tblFunc[a_] := Table[i + j, {i, 1, a}, {j, 1, a}];
Developer`PackedArrayQ[tblFunc[Unevaluated@a]]
]
now we see the expected result, since now a
is still symbolic INSIDE the function, we are back to square one, and now it is slow, since not packed. And since it is not packed, Compile is not used.
{0.437, False}
edit(2) Thanks to everyone for the answers, I think I learned allot from them.
Here is an executive summary, just to make sure I got everything ok.
edit(3)
Here are links I have specially related to hints to use to making Mathematica code runs faster.