in reply to Fly Subroutines on the Fly
But given the overhead of entering an anonymous function it is not worth looking for optimizations this way. Instead stop and think about "optional hooks" in your code. Where closures rock is as a way to provide optional hooks when you compile a function without paying any run-time price if the hook was not included. So instead of doing run-time checks for whether debugging (for instance) is on, make the debugging checks be inserted or not when you create the string to eval for the sub.sub Half { my $n = shift; my $SubBody = Power_Of_Two( $n ) ? "return \$_[$[] >> Log2( $n );" : "return int( \$_[$[] / $n );"; my $code = "sub { $SubBody }"; my $sub = eval $code; die "Code '$code' gave error $@" if $@; return $sub; }
As the rule goes, optimize on the level of big design decisions, not faster local constructs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: RE (tilly) 1: Fly Subroutines on the Fly
by BlaisePascal (Monk) on Sep 19, 2000 at 06:30 UTC | |
by chromatic (Archbishop) on Sep 19, 2000 at 07:09 UTC | |
by tilly (Archbishop) on Sep 19, 2000 at 15:23 UTC | |
by BlaisePascal (Monk) on Sep 19, 2000 at 22:29 UTC | |
by Adam (Vicar) on Sep 19, 2000 at 22:41 UTC | |
by merlyn (Sage) on Sep 19, 2000 at 22:32 UTC | |
by tilly (Archbishop) on Sep 19, 2000 at 23:05 UTC | |
by Adam (Vicar) on Sep 19, 2000 at 19:37 UTC | |
by tilly (Archbishop) on Sep 19, 2000 at 19:49 UTC |