4
$\begingroup$

A friend of mine found a huge difference in runtime speed between these two

n=10^7;
(*1*) Timing[x = Table[Sin[2.2];, {i, n}];]
(*2*) Timing[x = Table[Sin[2.2], {i, n}];]

On computer, I get

{5.682385, Null}
{0.205839, Null}

Just by removing the semi-colon, it makes the code run 20 times faster. How do I understand why that is?

$\endgroup$
3
  • 6
    $\begingroup$ Presumably it is faster to construct and store into a packed machine double array than a regular expression array comprised of Null. $\endgroup$ Dec 12, 2014 at 19:20
  • $\begingroup$ @DanielLichtblau answer? $\endgroup$
    – Yves Klett
    Dec 13, 2014 at 11:28
  • 1
    $\begingroup$ @YvesKlett Giving it a go, but it's far from a complete answer. $\endgroup$ Dec 13, 2014 at 21:50

1 Answer 1

6
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ I think it actually does compile in the fast case. SystemOptions["CompileOptions"] yields {"CompileOptions" -> {..., "TableCompileLength" -> 250}}. $\endgroup$
    – Michael E2
    Dec 13, 2014 at 22:39
  • $\begingroup$ I guess it is lack of the autocompilation, not packed arrays. Table[Sin[2.2] + Sin[2.3];, {i, n}] is 2 times slower then Table[Sin[2.2];, {i, n}] with the same output. But, of course, autocompilation and packed arrays are closery related to each other. $\endgroup$
    – ybeltukov
    Dec 14, 2014 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.