#!/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; }