in reply to Re^3: speeding up a regex
in thread speeding up a regex
modern perls have optimisations to avoid recompiling patterns if the interpolated variables haven’t changedModern perls are even smarter than that. They look at the string after interpolation, and if that's the same, the regex isn't executed:
Perl won't recompile the second regex, since while the values of both $foo and $bar have changed, the value of "$foo$bar" hasn't.$ cat xx #!/usr/bin/perl use strict; use warnings; foreach my $x (["foo", "bar"], ["fo", "obar"]) { my ($foo, $bar) = @$x; "" =~ /$foo/; "" =~ /$foo$bar/; } __END__ $ perl -Dr xx 2>&1 | perl -nle 'if (/EXECUTING/ .. eof) {print if/^Compiling/}' Compiling REx `foo' Compiling REx `foobar' Compiling REx `fo'
|
|---|