Apart from that, I'm not surprised you find your code as shown to be slow... it has an infinite loop! You should recast it as something like: ...
Um. No? I've played around with my implementation a couple of different ways. I've not yet encountered it failing to terminate with the desired result.
update: I didn't realise at first that you only cared about the depth, not what's at each level. In which case, the following snippet will be pretty efficient (since it is blockless):
Ah, yeah that's a little faster, about 15% faster than the version with the block.

By the way, in benchmarking these two methods I found that getting the stack depth (either way) is really quite fast, I had to do a million repetitions of my test code (each run had a stack depth of 3). That's much less overhead than my naive expectations would have predicted.

Update: I played around with the two versions a little more and found that really the difference between the block and the inline versions is only about 4%. My block implementation had some extra overhead unrelated to the 'blockiness' of it.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."


In reply to Re^2: Getting the size of the call stack (efficiently) by DrWhy
in thread Getting the size of the call stack (efficiently) by DrWhy

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.