Re: A Reap Sow question
- To: mathgroup at smc.vnet.net
- Subject: [mg65232] Re: A Reap Sow question
- From: albert <awnl at arcor.de>
- Date: Sun, 19 Mar 2006 03:19:09 -0500 (EST)
- References: <200603170513.AAA09771@smc.vnet.net> <4EF26559-3634-461D-9E5E-6522C06A6751@mimuw.edu.pl> <dvgsf0$9qe$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi János,
> So, my situation is similar to Trurl's. Right now I am appending to
> a list and as I am adding more and more items to it by using
> AppendTo, I have to pull back some earlier members of the list,
> because they are needed to create the new members of the list. This
> method works satisfactory for the first 60 - 80 members of the list
> and my G5 duo core constructor - although it is NOTHING compared to
> Trurl - is happy to churn out them in less than a minute. As soon as
> I have about 110 items, it takes half an hour to put one new item
> into it. So I was thinking to give up on AppendTo and use Reap Sow
> to collect the list elements, but on the same time I needed to look
> into the list for earlier elements, because I needed them for
> construction.
I'm not sure if I fully understood what you want and I also am not sure
whether my following suggestion will be a solution concerning speed, but
since AppendTo is a known speed-killer it might be worth a try.
Have you tried to use DownValues instead of a real list? That has some
overhead ,too, but as far as my experience goes, you won't suffer much if
the number of entries gets big, at least not for a few hundert entries. To
find the last entry you might need to store the current max index in an
additional variable.
That is, instead of using:
(* initialization *)
collection = {}
(* append element *)
AppendTo[collection,element]
use:
(* initialization *)
$maxindex = 0
container[$maxindex++] = element
accessing "list" elements is straightforward then:
collection[[i]] will be container[i]
and
Last[collection] will be container[$maxindex]
of course you could encapsulate all this in some functions that mimic the
regular list interface (e.g. by defining functions part[], appendto[],
first[] and last[]) if it turns out to solve your problem. It might also
well be that there are better ways to do it. Anyway it will be possible to
access previously defined entries in a while loop unlike when using the
Reap-Sow version and my guess is it will be considerably faster than
AppendTo...
hth,
albert
- References:
- A Reap Sow question
- From: János <janos.lobb@yale.edu>
- A Reap Sow question