Re: plotting many curves {best times}
- To: mathgroup at smc.vnet.net
- Subject: [mg108382] Re: plotting many curves {best times}
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Mon, 15 Mar 2010 07:01:57 -0500 (EST)
- References: <hnicrc$sv5$1@smc.vnet.net>
Hi,
> any clue why Thread outperforms?
> ------------------------------------------------------------------------------------
> Timing[circles=Thread[{xoi+ri*Cos[t],yoi+ri*Sin[t]}];]
> {0.001,Null}
> --------------------------------------------------------------------------------------
> circles=Table[Null,{n}];
> Timing[For[i=1,i<=n,i++,
> circles[[i]]={xoi[[i]]+ri[[i]]*Cos[t],yoi[[i]]+ri[[i]]*Sin[t]}];]
> {0.002,Null}
> ---------------------------------------------------------------------
> Timing[circles=Table[{xoi+ri*Cos[t],yoi+ri*Sin[t]},{n}];]
> {0.077989,Null}
> ---------------------------------------------------------------------------------------------------
1) you need to construct examples which run much longer to get
reasonable comparations
2) your last example doesn't do the same thing as the others. If
programmed correctly Thread and Table are much faster as the explicit
loop, which is what could be expected:
n = 10^5;
xoi = RandomReal[{-10, 10}, {n}];
yoi = RandomReal[{-10, 10}, {n}];
ri = RandomReal[{0, 10}, {n}];
In[31]:= Timing[circles = Thread[{xoi + ri*Cos[t], yoi + ri*Sin[t]}];]
Out[31]= {1., Null}
In[32]:= circles = Table[Null, {n}];
Timing[For[i = 1, i <= n, i++,
circles[[i]] = {xoi[[i]] + ri[[i]]*Cos[t],
yoi[[i]] + ri[[i]]*Sin[t]}];]
Out[33]= {73.282, Null}
In[34]:= Timing[
circles =
Table[{xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}, {i,
n}];]
Out[34]= {1.046, Null}
The general rule is: use internal functions as much as possible and
avoid explicit loops and Append/AppendTo. Other than that, experimenting
is sometimes necessary to find which construct is fastest for a specific
problem.
hth,
albert