Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:54:40+00:00 2026-05-25T21:54:40+00:00

I am assuming there might be an efficiency difference between: if (index($string, abc) <

  • 0

I am assuming there might be an efficiency difference between:

if (index($string, "abc") < -1) {}

and

if ($string !~ /abc/) {}

Could someone confirm that this is the case based on how both are implemented in Perl (as opposed to pure benchmarking)?

I can obviously make a guess as to how both are implemented (based on how I would write both in C) but would like more informed answer ideally based on actual perl sourcecode.


Here’s my own sample benchmark:

                          Rate regex.FIND_AT_END    index.FIND_AT_END
regex.FIND_AT_END     639345/s                   --                 -88%
index.FIND_AT_END    5291005/s                 728%                   --
                          Rate regex.NOFIND         index.NOFIND
regex.NOFIND          685260/s                   --                 -88%
index.NOFIND         5515720/s                 705%                   --
                          Rate regex.FIND_AT_START  index.FIND_AT_START
regex.FIND_AT_START   672269/s                   --                 -90%
index.FIND_AT_START  7032349/s                 946%                   --
##############################
use Benchmark qw(:all);

my $count = 10000000;
my $re = qr/abc/o;
my %tests = (
    "NOFIND        " => "cvxcvidgds.sdfpkisd[s"
   ,"FIND_AT_END   " => "cvxcvidgds.sdfpabcd[s"
   ,"FIND_AT_START " => "abccvidgds.sdfpkisd[s"
);

foreach my $type (keys %tests) {
    my $str = $tests{$type};
    cmpthese($count, {
        "index.$type" => sub { my $idx = index($str, "abc"); },
        "regex.$type" => sub { my $idx = ($str =~ $re); }
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:54:40+00:00Added an answer on May 25, 2026 at 9:54 pm

    Take a look at the function Perl_instr:

     430 char *
     431 Perl_instr(register const char *big, register const char *little)
     432 {
     433     register I32 first;
     434 
     435     PERL_ARGS_ASSERT_INSTR;
     436 
     437     if (!little)
     438         return (char*)big;
     439     first = *little++;
     440     if (!first)
     441         return (char*)big;
     442     while (*big) {
     443         register const char *s, *x;
     444         if (*big++ != first)
     445             continue;
     446         for (x=big,s=little; *s; /**/ ) {
     447             if (!*x)
     448                 return NULL;
     449             if (*s != *x)
     450                 break;
     451             else {
     452                 s++;
     453                 x++;
     454             }
     455         }
     456         if (!*s)
     457             return (char*)(big-1);
     458     }
     459     return NULL;
     460 }
    

    Compare with S_regmatch. It seems to me that there is some overhead in regmatch compared to index 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I know there is a git-daemon running at git://git.mycompany.com , how can
Are there any way to access or set iphone's alarm? Im assuming if this
Is there a cleaner way to do the following, assuming that I have a
is there any documentation on changes that i might hit when migrating an app
I realize there's already been several questions like this, but I think my case
Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
Assuming there are 5 inputs in web form <input name='the_same[]' value='different' /> <input name='the_same[]'
Should I be separating my js for each page assuming there is no overlap
I'm currently estimating a new project. My high level estimate assuming there was one
Assuming Windows, is there a way I can detect from within a batch file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.