Out of curiosity (and for the good purpose of delaying more serious work) I ran some bechmarks to find the break-even point of kyle's logarithmic method. I found it at a depth of 15 on my machine, frankly much lower than I expected but still a bit high for practical purposes. Calls don't often nest that deep.

The relative efficiency varies from about 1:2 in favor of the linear method at depth 2 (the lowest that can be easily benchmarked), to, well, arbirtary much in favor of the logathithmic method. 1:3 at 100, 1:30 at 1000.

The full story is a bit lengthy

#!/usr/local/bin/perl use strict; use warnings; $| = 1; use Vi::QuickFix; use Benchmark qw( cmpthese); goto bench; check: { printf "drwhy: %d, kyle: %d\n", depth_drwhy(), depth_kyle(); for my $depth ( 0, 1, 2, 3, 4, 10, 100, 1000 ) { call_at_depth( $depth); } } exit; bench: { for my $depth ( 0, 1, 8, 13, 98, 998 ) { cmp_at_depth( $depth); } } exit; ################################################################## sub call_at_depth { no warnings 'recursion'; my ( $depth) = @_; return call_at_depth( $depth - 1) if $depth; printf "drwhy: %d, kyle: %d\n", depth_drwhy(), depth_kyle(); } sub cmp_at_depth { no warnings 'recursion'; my ( $depth, $time) = @_; return cmp_at_depth( $depth - 1, $time) if $depth; defined $time or $time = 1; printf "at depth %d\n", depth_drwhy(); cmpthese( -$time, { drwhy => \ &depth_drwhy, kyle => \ &depth_kyle, }, ); print "\n"; } sub depth_drwhy { my $depth = 0; ++ $depth while caller $depth; $depth; } sub depth_kyle { my $depth = 1; $depth *= 2 while ( caller $depth ); my $max = $depth; my $min = $depth / 2; while ( $min < $max - 1 ) { my $mid = ($min + $max)/2; caller $mid ? $min : $max = $mid; } $max; }
And the output...
at depth 2 Rate kyle drwhy kyle 52194/s -- -47% drwhy 99211/s 90% -- at depth 3 Rate kyle drwhy kyle 51691/s -- -43% drwhy 91391/s 77% -- at depth 10 Rate kyle drwhy kyle 40959/s -- -22% drwhy 52193/s 27% -- at depth 15 Rate drwhy kyle drwhy 39267/s -- -1% kyle 39628/s 1% -- at depth 100 Rate drwhy kyle drwhy 4970/s -- -77% kyle 21290/s 328% -- at depth 1000 Rate drwhy kyle drwhy 94.2/s -- -97% kyle 3556/s 3673% --

In reply to Re^2: Getting the size of the call stack (efficiently) by Anno
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.