in reply to efficient string translation?
If all you're trying to accomplish is to reduce the effects of an eval in a tight loop, you could cache it.
{ my %pwdrots; sub pwdrot { my $pwd = shift; my $degree = (@_ > 0) ? ((shift) % 94) : 47; if ($degree == 0) { return $pwd; } if (length($str) == 0) { return $pwd; } unless ($pwdrots{$degree}) { my $rangstr = "\\" . sprintf("%03lo",$degree+33) . "-\\176\\041- +\\" . sprintf("%03lo",$degree+32); $pwdrots{$degree} = eval "sub { $_[0] =~ tr[\041-\176][$rangstr] + }" } $pwdrots{$degree}->($pwd); return $pwd; } }
The idea is that you generate an anonymous sub which does the tr, once that is compiled once, you don't need to compile it again (for the same $degree).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: efficient string translation?
by 5mi11er (Deacon) on Jan 27, 2005 at 19:59 UTC |