Consider the following example
n = 10^8;
AbsoluteTiming[
A = ConstantArray[1., n]; // anyFunc;
A[[2 ;; ;; 2]] = -1.;
]
{0.388108, Null}
n = 10^8;
AbsoluteTiming[
B = ConstantArray[1., n];
B[[2 ;; ;; 2]] = -1.;
]
{0.738504, Null}
A == B
True
Why is the first approach faster? The only difference is, there is an arbitrary function outside the first one. I am using Mathematica 12 on windows 10 64 bit.
$HistoryLength=0
$\endgroup$DownValues[Out]
were totally cluttered... $\endgroup$B
but notA
that causes the tensor to be copied when some elements are changed to-1.
That the tracking would be turned off when one sets$HistoryLength = 0
makes sense; but why a reference toB
exists but not toA
-- or why a reference toB
exists at all -- I cannot explain. $\endgroup$$HistoryLength = 0
can be relevant inwolframscript
, cause there you cannot have access to history even though is set to infinity by default. It would make sense to set it to zero if it gives performance increase. $\endgroup$