Here is a function that take some time to evaluate:
n = 1000;
coeffs = RandomVariate[NormalDistribution[], n];
f[x_] := Sum[coeffs[[k]] Sin[k x]/k, {k, 1, n}];
If I just plot the function with 1000 sample points, it takes about 0.24 seconds on my machine:
Plot[Evaluate@f[x], {x, 0, 2 Pi}, PlotPoints -> n, MaxRecursion -> 0] // Timing
On the other hand, if I try to collect 1000 samples of the function into a table, it takes five times as long:
Table[Evaluate@f[x], {x, 0, 2 Pi, 2 Pi/n // N}] // Timing
(Without the // N
Mathematica tries to evaluate things symbolically, which is even slower.)
What is going on, and how can I make the Table
as fast as the Plot
?
Evaluate
from yourTable
expression. Also, this result is entirely dependent on theMaxRecursion
setting. If you remove this, or leave it in but increase its value, the plot takes longer than the table. $\endgroup$MaxRecursion -> 0
expecting that thePlot
would evaluate exactly 1000 regularly spaced sample points, just like theTable
. (Well, theTable
actually evaluates 1001 points, but close enough.) Is that not true? $\endgroup$Table
can be slow. Not sure why now. Yes, you are right,MaxRecursion -> 0
makes sure no more points are taken. $\endgroup$