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 8723781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:42:03+00:00 2026-06-13T07:42:03+00:00

I got a pretty simple regular expression I am using %%(products?)%% Now I want

  • 0

I got a pretty simple regular expression I am using

%%(products?)%%

Now I want it to be able to match both products? and Products?. The obvious answer is to use the CASE_INSENSITIVE tag when compiling a pattern:

Pattern.compile("%%(products?)%%", Pattern.CASE_INSENSITIVE)

But on the documentation it says “Specifying this flag may impose a slight performance penalty.” I therefore thought of an alternative, without the flag:

Pattern.compile("%%([Pp]roducts?)%%")

My question is: Which one would have better performance?

  • 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-06-13T07:42:04+00:00Added an answer on June 13, 2026 at 7:42 am

    Actually, there is a significant difference between the methods.

    While Pattern.compile("%%(products?)%%", Pattern.CASE_INSENSITIVE) might seem less efficient than Pattern.compile("%%([Pp]roducts?)%%") at first glance, it’s internal functioning is not exactly that of comparing each character with both their lower’ and uppercase counterparts; What actually happens is that the first method does a range-check with Unicode’s lower’ and uppercase blocks, while the second makes literal comparison.

    I don’t have knowledge much deeper than that, but the important part is this simple, but very interesting test (results on my machine included at the end):

    String base = "I have a product that is the product of my hard work." 
      + "Products are always nice, because I can win cash if I sell my products.\n" 
      + "The product of me making my product is cash, because cash is the product of selling my product.\n" 
      + "With the cash I win with my product, I can buy other people's products.";
    
    int processRepeats = 1000000; //One million runs, enough to take time for each clocking.
    int averageRepeats = 10;
    
    long averager = 0;
    int count = 0;
    
    //Switch the commenting to test the opposing method.
    Pattern p = Pattern.compile("products?", Pattern.CASE_INSENSITIVE);
    //Pattern p = Pattern.compile("[Pp]roducts?");
    Matcher m;
    long clocking;
    for (int i = 0; i < averageRepeats; i++) {
      clocking = System.nanoTime();
      for (int ii = 0; ii < processRepeats; ii++) {
        m = p.matcher(base); //Here because the "base" would change in a real environment.
        while (m.find()) {
          count++;
        }
      }
      clocking = System.nanoTime() - clocking;
      averager += clocking;
      //System.out.printf("This method found %9d matches in %15d nanos [%9.3f ms]\n", count, clocking, clocking / 1000000f);
    }
    System.out.printf("This method averages %15d nanos [%16.3f ms] for %d times executing %d runs.\n",
    averager / averageRepeats, (averager / (float) averageRepeats) / 1000000f, averageRepeats, processRepeats);
    
    //RESULTS ON MY MACHINE:
    
    //FIRST METHOD: [3 runs to demonstrate/guarantee consistency]
    //This method averages      5024404693 nanos [        5024,404 ms] for 10 times executing 1000000 runs.
    //This method averages      5021385539 nanos [        5021,386 ms] for 10 times executing 1000000 runs.
    //This method averages      5017170143 nanos [        5017,170 ms] for 10 times executing 1000000 runs.
    
    //SECOND METHOD: [same deal]
    //This method averages      5806310774 nanos [        5806,311 ms] for 10 times executing 1000000 runs.
    //This method averages      5809879747 nanos [        5809,880 ms] for 10 times executing 1000000 runs.
    //This method averages      5804277386 nanos [        5804,277 ms] for 10 times executing 1000000 runs.
    

    As you can see, not only the first method is faster (at last depending on the machine it’s running), but also the performance difference of almost 800 ms (8/10 s), considering a large amount of runs, might not be as negligible an impact as one might think!

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

Sidebar

Related Questions

That's it. It's pretty simple. I've got an edmx and want to be able
I've got a question that probably has a pretty simple answer, but I didn't
I'm sure the answer is pretty simple, but I got stuck in this: Welcome
I've got a pretty simple problem that has a solution I'm not able to
I've got a pretty simple app I'm trying to make. What I want to
Pretty simple question, perhaps not so simple answer though: I've got a clear view
I've got a pretty simple question (and tentative answers), I just want to see
I've got a pretty simple problem, I think. I'm using WPF in Visual Basic
Pretty simple premise. I've got a floating image and I want to reveal the
Building an application which started as fairly simple, but now got pretty complicated. I

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.