Re: plotting many curves {best times}
- To: mathgroup at smc.vnet.net
- Subject: [mg108332] Re: plotting many curves {best times}
- From: eric g <eric.phys at gmail.com>
- Date: Sun, 14 Mar 2010 05:13:17 -0500 (EST)
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 > > > >