Re: Lists and speed
- To: mathgroup at smc.vnet.net
- Subject: [mg30168] Re: Lists and speed
- From: "Orestis Vantzos" <atelesforos at hotmail.com>
- Date: Wed, 1 Aug 2001 02:19:23 -0400 (EDT)
- Organization: National Technical University of Athens, Greece
- References: <9k5qp5$ha6$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
You need to get into using Compile to really speed things up. Also, although I don't really remember your case, wipe any traces of Part from your code. Try converting everything into List operations, using Dot, Inner, Outer etc. Many newbies use For or Do loops over the index of a list, accessing the elements with Part, like a[[i]]. This is slow. Mathematica spped up rule no.1 is "never break the list". Orestis "Stuart Humphries" <s.humphries at bio.gla.ac.uk> wrote in message news:9k5qp5$ha6$1 at smc.vnet.net... > Hi, > > About a month ago I submitted a question about lists and received some > very useful help (summarised below). However, although my program (a > coupled map lattice for population dynamics) now works well with Ted > Ersek's modifications, it still takes over 20 minutes (compared to the > original 40 minutes) to run. > A colleague has just coded the same thing in another system, which runs in around 40 _seconds_...is this unusual? > I was happy to trade-off speed for faster development time compared to > Pascal etc, but the difference here seems so huge that I'm concerned that > my Mathematica programming is still way-off. > Any thoughts welcome. > Thanks > Stuart > > > >The program considers a 10 by 10 lattice of populations, and implements > >equations for population growth and then manipulated the lattice (list) to > >incorporate dispersal between populations, the whole process is repeated > >for about 1200 'generations' , and this itself form part of a larger (100+ > >) repetition. > > >Stuart Humphries had a Mathematica program that was running rather slow. > >The poor performance was most likely do to the use of AppendTo[data, ..] > >inside a Do loop. > >Instead of using AppendTo[data, ...] use the following: > > > > > >Do[ ( > > blah; blah; ....; > > data = {data, stuff} > >), {n, 500}] > >Flatten[data] > >------------------------ > >However, that will not work if the final result is supposed to be a matrix. > >In that case do the following instead. > > > > > >Module[{h}, > > Do[ ( > > blah; blah; ....; > > data = h[data, stuff] > > ), {n, 500}] > > List@@Flatten[data] > >] > > > > > >-------------- > >Cheers, > >Ted Ersek > > > > Dr Stuart Humphries > FBA/NERC Research Fellow > > Division of Environmental and Evolutionary Biology > Graham Kerr Building > University of Glasgow > Glasgow G12 8QQ > Tel: +44 (0)141 330 6621 > Fax: +44 (0)141 330 5971 > http://www.gla.ac.uk/Acad/IBLS/DEEB/sh > >