Here is the test result:
use Benchmark qw(:all) ; my $count = -1; my $str1 = "this is a test string"; my $str2 = "test"; my $str3 = qr/test/; cmpthese($count, { 'type1' => sub { if ( index($str1, $str2) != -1 ) { 1 } }, 'type2' => sub { if( $str1 =~ $str3 ) { 1 } }, });
Result (when a match occurs):
Rate type2 type1 type2 1747627/s -- -70% type1 5770465/s 230% --
To be able to conclude, the test does not match:
my $str2 = "text"; my $str3 = qr/text/;
Result (if the match fails):
Rate type2 type1 type2 1857295/s -- -67% type1 5560630/s 199% --
Output:
The index function is much faster than the regular expression.
source share