in reply to Abbreviation regex?

fishbot_v2 has my vote for best solution within the constraints. However, if you were doing this repeatedly, using index might be an interesting approach. I've coded fishbot_v2's solution into the regex sub below. I did several runs and pasted a pretty typical result.

use Benchmark qw/:all/; sub regex { return ($_[1] =~ m/^$_[0]/) ? 1 : 0; } sub index_func { return (index($_[1], $_[0]) == 0) ? 1 : 0; } cmpthese ( 500000, { 'regex match' => sub { regex('abbr', 'abbreviation') or die }, 'index match' => sub { index_func('abbr', 'abbreviation') or die } +, 'regex miss' => sub { regex('anna', 'abbreviation') and die }, 'index miss' => sub { index_func('anna', 'abbreviation') and die +}, }); __END__ Rate regex match regex miss index miss index match regex match 680272/s -- -13% -36% -38% regex miss 781250/s 15% -- -27% -29% index miss 1068376/s 57% 37% -- -3% index match 1101322/s 62% 41% 3% --

As you can see, using index for this is much faster, regardless of whether you are more likely to match or miss.

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet