I've been trying to optimize a bit of code the past couple of days, but think I might be misunderstanding how compilation on certain target functions works. As a most barebones example, I've attached a screenshot below of the speeds for array creation using ConstantArray, Table, and the compiled versions of each.
We see that ConstantArray is nearly 10x faster than Table in this most simple case (perhaps not surprisingly given that simplicity). When we try to compile, ConstantArray cannot be compiled, and so the time taken just increases because of overhead, I assume. However, the compiled Table call also increases in time. This is unexpected to me, so I figure I must be doing something wrong in this comparison. Any thoughts on how to yield a speed increase from compiling Table, if it is possible in such a situation? I should also note that using Array is categorically slower than both other options, for both uncompiled, and compiled.
Compile[{}, Table[1., 10000000]]
is fully compiled, butCompile[{}, Table[1., 10^7]]
isn't. One has to use the old syntaxCompile[{}, Table[1., {10^7}]]
. Tested in 12.1.0 for Linux x86 (64-bit) (March 4, 2020) (Wolfram Cloud). $\endgroup$Compile[{}, Table[1., {10000000}], CompilationTarget -> "C", RuntimeOptions -> "Speed"]
takes four times longer thanConstantArray[1., {10000000}]
. Hard to say what goes wrong there. $\endgroup$