The main reason why
x = 0.1; Do[x = 3 x (1 - x), 300] // AbsoluteTiming // First
0.000393629
and
lf[x_] := 3 x (1 - x)
x = 0.1; Do[x = lf[x], 300] // AbsoluteTiming // First
Nest[lf[#] &, .1, 300] // AbsoluteTiming // First
0.000643239
0.000691691
are slower than
Nest[3 # (1 - #) &, .1, 300] // AbsoluteTiming // First
0.000161729
is the fact, that the last Nest
can and does make use of autocompilation.
The default NestCompileLength
is 100:
SystemOptions["CompileOptions" -> "NestCompileLength"]
{"CompileOptions" -> {"NestCompileLength" -> 100}}
Autocompilation can be switched off for Nest
by setting its compile length to Infinity
.
SetSystemOptions["CompileOptions" -> "NestCompileLength" -> Infinity]
Now
Nest[3 # (1 - #) &, .1, 300] // AbsoluteTiming // First
0.000461127
is much slower and has a performance similar to the first Do
.
By using a precompiled version of lf
a performance that is even better than the pure function with Nest
's autocompilation can be achieved:
clf = Compile[{{x, _Real, 0}}, 3 x (1 - x)];
Nest[clf, .1, 300] // AbsoluteTiming // First
0.000128982
However, if the time needed for compiling is included
First@AbsoluteTiming[
clf2 = Compile[{{x, _Real, 0}}, 3 x (1 - x)];
Nest[clf2, .1, 300]
]
0.000187792
one can see that this uses more time in total than the pure function with autocompilation, as calling clf2
for every evaluation adds an additional overhead.
After switching autocompilation back on
SetSystemOptions["CompileOptions" -> "NestCompileLength" -> 1];
Nest
does need some extra time, because it tries to autocompile clf
now
Nest[clf, .1, 300] // AbsoluteTiming // First
0.000188127
NestCompileLength
is 100 by default. $\endgroup$RepeatedTiming
can be misleading. $\endgroup$Compile[{}, Nest[lf, .1, 300]]
doesn't change much -- still0.000382
even with increasedNestCompileLength
. Can you elaborate on misleading ofRepeatedTiming
? What is better then? $\endgroup$MainEvaluate[]
, and diminish or destroy any advantage in compiling. So probably neitherlf
norlf[#] &
is compiled, but3 # (1 - #) &
is. $\endgroup$