Perl: string Index function or regular expression - which is better and when?

I am writing several Perl scripts where I need to execute many lines. For instance:

my $str1 = "this is a test string"; my $str2 = "test"; 

To find out if $ str1 contains $ str2 - I found that there are 2 approaches:

Approach 1: use the index function:

 if ( index($str1, $str2) != -1 ) { .... } 

Approach 2: use a regex:

 if( $str1 =~ /$str2/ ) { .... } 

What's better? and when should we use each of them over the other?

+6
source share
3 answers

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.

+4
source

When I see code that uses index , I usually see index inside index inside index , etc. There are also more branches: "if found, look for it, otherwise, since you have not found, look for it." There would almost always be one regular expression. So, for me, I almost always use regex if there is no specific reason why I want to use index .

Unfortunately, most of the programmers I come across do not read the regular expression well, and therefore, for the convenience of maintenance, the index method should be used more than me.

+2
source

If you need subscript matching, use index . If you need a regular expression (with a special meaning for the regexp metacharacters), use =~ . Substring matching is usually faster, but regular expressions in Perl are pretty well optimized, and simple regular expressions can be surprisingly fast. Enjoy it for yourself.

For best performance with regular expressions, you can make sure that Perl does not compile the regular expression more than once: $str =~ /$str2/o .

0
source

Source: https://habr.com/ru/post/988812/


All Articles