RE: AppendTo VERY slow
- To: mathgroup at smc.vnet.net
- Subject: [mg35315] RE: [mg35279] AppendTo VERY slow
- From: "tgarza01 at prodigy.net.mx" <tgarza01 at prodigy.net.mx>
- Date: Mon, 8 Jul 2002 03:16:09 -0400 (EDT)
- Reply-to: tgarza01 at prodigy.net.mx
- Sender: owner-wri-mathgroup at wolfram.com
Indeed, AppendTo is an awfully slow function. It takes exponentially longer to execute with the list size. I would suggest a simple outlist = {outlist, elem} with a Flatten and Partition at the end. For example, In[1]:= xlist=Table[Random[Integer,{0,9}],{10000}]; ylist=Table[Random[Integer,{0,9}],{10000}]; I define a function to do things with Append To: In[2]:= app[n_]:=( outlist={};Do[elem={xlist[[i]],ylist[[i]]}; AppendTo[outlist,elem],{i,1,n} ]) In[3]:= app[10000]//Timing Out[3]= {45.37 Second,Null} It takes 45 seconds to process 10,000 elements. Now, with the approach I suggest, I define the function f (in order to use Fold, a very powerful function): In[4]:= f[x_,n_]:=outlist={outlist,{xlist[[n]],ylist[[n]]}} In[5]:= outlist={};Partition[Flatten[Fold[f,{},Range[10000]]],2];//Timing Out[5]= {0.49 Second,Null} which produces the same result almost 100 times faster. Tomas Garza Mexico City Original Message: ----------------- From: M.P.Croucher at Sheffield.ac.uk (Mike) To: mathgroup at smc.vnet.net Subject: [mg35315] [mg35279] AppendTo VERY slow I use lists a lot in mathematica and tend to use AppendTo[] a lot in my programs. Recently I wrote a function that i call over and over again and found that the results were coming very slowly and i MEAN slowly. I was doing Fourier Transforms and all kinds of stuff so I put it down to those at first but I have just put in a load of Print statements just after each part of the function to see what was taking so long. I was amazed to see that the Fourier Transforms were so quick and what was actually taking the time was a part of my function that collected the results togther in the form I wanted and outputted the result. It looks like this Do[ elem = {xlist[[count]], ylist[[count]]]}; AppendTo[outlist, elem]; , {count, 1, number} ]; It seems that as the list grows it gets slower and slower. Any tips on a way around this would be greatly appreciated (would speed my life up no end) Thank Mike -------------------------------------------------------------------- mail2web - Check your email from the web at http://mail2web.com/ .