Inline::C would likely be more complicated that it is worth. You may be able to squeeze some more uumph out of the perl version, as the poster above pointed out.

If you are recalculating this more than once (it doesn't look like you are) you could have the children notify the parent when their own depth changes. This way you wouldn't have to recalc the whole tree if just a couple changed.

If you are only doing this once, you could try some other optimizations. Keep two lists of children in the parent, one for object type #1's (which have variable depth) and the main list which has all objects. If the max depth of the #1s list is greater than one, just use that. If there are alot of #2 nodes, you save the function call overhead. Not great OO, but if you need the speed ...

You could try something like

sub depth { my $self = shift; return $self->{myDepth} = shift(sort map{ $_->depth(); } @{$self->{chi +ld}}[0:14]) + 1; }
To speed up the inner loop as well. That might have a touch of python syntax in it, but it is close to what you want.

In reply to Re: inline perl and recursion by jackdied
in thread inline perl and recursion by abitkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.