I will guess that the time needed to create a stock a packed array is significantly faster than that for filling a regular expression, even if every element is Null. For one, it is possible that Table
, being a function that holds it arguments, preprocesses to the point of knowing (well, suspecting) that everything is a machine double in the fast case, and mostly avoiding the main evaluator thereafter (in effect behaving as though one had used Compile
, to the level of C code). Also there is the actual storing: once we are in the packed array world it is only a matter of indexing into an array of machine numbers, and this should only involve fast memory accesses. With an ordinary expression the accessing, and hence setting, will be less direct.
There may be other wrinkles associated with producing two values per iteration (the sine, and Null
). If we create a table of exact values, like so, it is somewhere in between in speed: Timing[x3 = Table[Sin[2], {i, n}];]
. Moreover the difference is not entirely about doing two evaluations, because this next variant is still faster than the one with the semicolon: Timing[x4 = Table[Sin[2]; Sin[3], {i, n}];]
. So I guess there is something about handling the Null
that adds a bit of complexity.
Null
. $\endgroup$