in reply to Re^2: optimization help
in thread optimization help
Applying what I said in Re: optimization help,
my @functions = ( sub { # sin(x) my $in = shift; return sin($in); }, sub { # add(x,y) my ($left,$right) = @_; return ($left + $right); }, sub { # mul(x,y) my ($left,$right) = @_; return ($left * $right); }, sub { # log(x) my $in = shift; return ($in == 0) ? 0 : log(abs($in)); }, ); ... sub evalTree { my $intree = shift; my $param = shift; # A shortcut for the current node. for my $contents ($intree->{contents}) { return 0 unless defined $contents; if (ref($contents) eq 'CODE') { return $contents->( evalTree($intree->{left}, $param), evalTree($intree->{right}, $param), ); } if ($contents =~ /^[A-Za-z]$/) { return $param->{$_}; } return $contents; } }
If you need both the string and the compiled sub, save both instead of recompiling the strings over and over again.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: optimization help
by samtregar (Abbot) on Oct 15, 2007 at 23:06 UTC | |
|
Re^4: optimization help
by GSF (Acolyte) on Oct 16, 2007 at 16:24 UTC | |
by ikegami (Patriarch) on Oct 16, 2007 at 16:29 UTC | |
by GSF (Acolyte) on Oct 16, 2007 at 17:10 UTC |