Re: Re: plotting many curves {best times}
- To: mathgroup at smc.vnet.net
- Subject: [mg108362] Re: [mg108332] Re: plotting many curves {best times}
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Mon, 15 Mar 2010 00:06:20 -0500 (EST)
- Reply-to: hanlonr at cox.net
Your use of Table is slow because you are not producing the same result. You are performing the vector operations n times rather than steping through the n elements of the vectors.
n = 10^2;
xoi = RandomReal[{-10, 10}, {n}];
yoi = RandomReal[{-10, 10}, {n}];
ri = RandomReal[{0, 10}, {n}];
Timing[circles1 = Thread[
{xoi + ri*Cos[t], yoi + ri*Sin[t]}];]
{0.000599,Null}
circles2 = Table[Null, {n}];
Timing[For[i = 1, i <= n, i++,
circles2[[i]] =
{xoi[[i]] + ri[[i]]*Cos[t],
yoi[[i]] + ri[[i]]*Sin[t]}];]
{0.001025,Null}
Verifying that the results are the same
circles1 === circles2
True
Timing[circles3 =
Table[{
xoi + ri*Cos[t],
yoi + ri*Sin[t]},
{n}];]
{0.049196,Null}
Note that these results are different.
circles1 === circles3
False
Table does better if we step through the vectors to produce the same result
Timing[circles4 =
Table[{
xoi[[m]] + ri[[m]]*Cos[t],
yoi[[m]] + ri[[m]]*Sin[t]},
{m, n}];]
{0.000683,Null}
Verifying that the results are the same
circles1 === circles4
True
A variation with Table
Timing[circles5 =
Table[{xoi[[m]], yoi[[m]]} +
ri[[m]] {Cos[t], Sin[t]},
{m, n}];]
{0.000761,Null}
Verifying that the results are the same
circles1 === circles5
True
You can use Transpose in place of Thread for this
Timing[circles6 = Transpose[
{xoi + ri*Cos[t], yoi + ri*Sin[t]}];]
{0.000606,Null}
Verifying that the results are the same
circles1 === circles6
True
Bob Hanlon
---- eric g <eric.phys at gmail.com> wrote:
=============
Guys,
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}
---------------------------------------------------------------------------------------------------
On 03/13/2010 09:56 AM, Guido Tripaldi wrote:
> in this case just using "Table" (since the For cycle is just used in this case to increment an index), and without the need to null-initialize the array (since it is dynamically created during evalutation):
>
> (*--------initialization------------------*)
> n = 10^2;
> xoi = RandomReal[{-10, 10}, {n}];
> yoi = RandomReal[{-10, 10}, {n}];
> ri = RandomReal[{0, 10}, {n}];
>
> n = 10^2;
> circles =
> Table[{xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}, {i,
> n}];
>
> (*---------------displaying--------------------*) \
> ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black]
>
>
>
> using Table you gain also a little extra performance:
>
> Timing[For[i = 1, i<= n, i++,
> circles[[i]] = {xoi[[i]] + ri[[i]]*Cos[t],
> yoi[[i]] + ri[[i]]*Sin[t]}]]
>
> {0.001701, Null}
>
>
>
> Timing[circles =
> Table[{xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}, {i,
> n}];]
>
> {0.001162, Null}
>
>
> Cheers,
> G
>
>
> Il giorno 13/mar/2010, alle ore 13.57, eric g ha scritto:
>
>
>> Hello Group,
>>
>> I know I should avoid For cycles in mathematica, but I am C person...
>> how to do this without For
>>
>> (*--------initialization------------------*)
>> n = 10^2;
>> xoi = RandomReal[{-10, 10}, {n}];
>> yoi = RandomReal[{-10, 10}, {n}];
>> ri = RandomReal[{0, 10}, {n}];
>> -----------------------------------
>> (*
>>
>> n=10^2;
>> Clear[circles];
>> circles = Table[Null, {n}];
>> For[i = 1, i<= n, i++,
>> circles[[i]] = {xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}]
>>
>> (*---------------displaying--------------------*)
>> ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black]
>>
>>
>>
>>
> ---
> Guido Tripaldi
>