in reply to Getting the size of the call stack (efficiently)

Without knowing any magic, you could check at intervals other than 1.

my $depth = 1; $depth *= 2 while ( caller $depth ); my $max = $depth - 1; my $min = $depth / 2 + 1;

Then do a binary search from there. You can probably get a bit faster if you know in advance about where your depth will be.

Update: Set starting depth to nonzero as suggested by Anno (though maybe 2 is a better value than 1 so that $depth / 2 + 1 is an integer).

Replies are listed 'Best First'.
Re^2: Getting the size of the call stack (efficiently)
by Anno (Deacon) on Feb 27, 2007 at 18:39 UTC
    That looks promising (except the starting value for $depth should be nonzero :).

    Anno

      Good catch! Fixed, thanks.

Re^2: Getting the size of the call stack (efficiently)
by Anno (Deacon) on Feb 27, 2007 at 20:37 UTC
    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