Monks,
Hey all, i've written a sub that I need to tune for performance. Every bit of speed I can squeeze out of this sub will help!
I have a message string that contains a section for the substitution of random characters, indicated by {\d2} for 2 random digits. This can also be expanded to multiple sets of digits containing multiple digits for more flexibility. I send each message to a sub and it then looks for the presense of the replace string and replaces with the random digit as needed. Any help on speed would be appreciated!
Example Input: "This is a message! {\d3} --- {\d2}"
Output: "This is a message! 234 --- 31"
sub substitute { my $message = shift; my @numeric_matches = ($message =~ m/\{\\d\d+\}/g); foreach (@numeric_matches) { my $substitution = $_; my ($substitution_count) = (substitution =~ m/(\d+)/); my $number; for (1..$substitution_count) { $number .= $numeric_chars[int rand @numeric_chars]; } $substitution =~ s/\\/\\\\/g; $message =~ s/$substitution/$number/ee; } return $message; }
In reply to Speed Improvement by Nar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |