RE: AppendTo VERY slow
- To: mathgroup at smc.vnet.net
- Subject: [mg35303] RE: [mg35279] AppendTo VERY slow
- From: "Harvey P. Dale" <hpd1 at nyu.edu>
- Date: Mon, 8 Jul 2002 03:15:29 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Mike: This is a well-known problem. David Wagner wrote about it under a heading, "Avoid Append and Prepend," in Power Programming with Mathematica: The Kernel. He also suggested several ways around the problem. For your purposes, his suggestion of using a linked-list structure is probably best. To do that, make each existing list the first element and each new value the second element of the new list. For example, if the values are 1, 2, 3, 4, 5, and 6, make the first list {1}, the second list {{1},2}, the third list {{{1},2},3}, and so forth up to {{{{{{1},2},3},4},5},6}. Then, when you're done, Flatten the final list. Hope that helps. Best, Harvey Harvey P. Dale University Professor of Philanthropy and the Law Director, National Center on Philanthropy and the Law New York University School of Law Room 206A 110 West 3rd Street New York, N.Y. 10012-1074 -----Original Message----- From: M.P.Croucher at Sheffield.ac.uk [mailto:M.P.Croucher at Sheffield.ac.uk] To: mathgroup at smc.vnet.net Subject: [mg35303] [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 ________________________________________________________________________ This email has been scanned for all viruses by the MessageLabs SkyScan service. For more information on a proactive anti-virus service working around the clock, around the globe, visit http://www.messagelabs.com ________________________________________________________________________