In article
<05699536-8456-4c5f-9463->,
Jason Cavett <> wrote:
> On Oct 23, 2:45*pm, Jason Cavett <jason.cav...@gmail.com> wrote:
> > On Oct 23, 2:39*pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > > In article
> > > <83663190-bae3-4a6b-8921-d3a0dc60a...@26g2000hsk.googlegroups.com>,
> > > *Jason Cavett <jason.cav...@gmail.com> wrote:
> >
> > > > On Oct 23, 12:00*pm, Jason Cavett <jason.cav...@gmail.com> wrote:
> > > > > In my original code, I implemented a method that searched through a
> > > > > tree structure to find children of a certain type beneath a parent
> > > > > node. *This is demonstrated in the following method. *Note that
> > > > > this method is in DataModel (which is a node in the tree, and an
> > > > > abstract class that has to be implemented by any node in the tree).
> > > [...]
> > > > > * * * * synchronized (childrenLock) {
> > > [...]
> > > > Just for the record, I profiled both methods, and the visitor pattern
> > > > takes approx. 3 times longer to perform the same task. *My guess is
> > > > method overhead.
> >
> > > Was the original synchronized, also?
[...]
> > Ah. *Yes it was. *I forgot to put that in the second method when I was
> > typing into the newsgroup. *Sorry about that. *(And, for the record, I
> > tried to take out the synchronization block just to see what would
> > happen, and there were no speed improvements.)
>
> So, I've now also tried to use a stack to implement recursion and came
> up with this.
>
> public final void visit(Visitor visitor) {
> Deque<DataModel> stack = new ArrayDeque<DataModel>();
> stack.push(this);
>
> while (stack.size() > 0) {
> DataModel current = stack.pop();
> visitor.startVisit(current);
>
> Enumeration<DataModel> children = current.children();
> while (children.hasMoreElements()) {
> stack.push(children.nextElement());
> }
> }
> }
>
> However, although it's slightly faster (via profiling), there's no
> real noticeable change. The only method being called here is
> startVisit (I don't use stopVisit right now, so I decided to remove it
> for the moment). This removes the need for synchronization which is
> nice, but that's about it.
>
> I can't figure out why this is THAT much slower.
I would expect the double dispatch to be only fractionally slower.
Presumably, your original getAllDescendentsOfType() traversed the data
model recursively, examining each node only once. Is it possible that
your visitor is visiting some of the same nodes repeatedly?
--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/