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

  • SEARCH
  • Home
  • 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 6866063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:08:00+00:00 2026-05-27T03:08:00+00:00

I found this in some code I wanted to optimize. Here is the snipet:

  • 0

I found this in some code I wanted to optimize.
Here is the snipet:

tempString = bigBuffer.replaceAll("\\n", "");
tempString = tempString.replaceAll("\\t", "");

Then I decided to use the regex wisely and I did this:

tempString = bigBuffer.replaceAll("[\\n\\t]", "");

Then a friend told me to do this instead:

tempString = bigBuffer.replaceAll("\\n|\\t", "");

Since I like to know the result of my changes I did a test to verify if it was a good optimization. So, the result with (java version “1.6.0_27”) is with the first code being the reference 100%.

With the pipe it is 121% so it took more time to perform the task.

With the square bracket it is 52% so it took less time to perform the task.

Why does the regex behave differently where it should be the same?

Martin

  • 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-27T03:08:00+00:00Added an answer on May 27, 2026 at 3:08 am

    The first code snippet looks through bigBuffer twice, the first time replacing the new lines, and the second time replaces the tabs.

    The second code snippet would search through bigBuffer only once, checking to see if each character is one or the other. This would result in the speed finishing in only half the time.

    The code snippet in the third place is probably poorly compiled, and results in a particularly bad version of the first code’s algorithm, though I could not say for sure without examining the path through the regex compilation carefully.

    Excellent work on the testing though. Relative timing (percent-based) is useful, absolute timing (millisecond or some such) is not.

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

Sidebar

Related Questions

I just found this in some old code, and I'm not sure what it
I found this statement is some old code and it took me a second
This is some code I found on the internet. I'm not sure how it
I'm trying to parse a html doc using some code I found from this
in some CSS code I found out this type of selector div#someid Is this
I was reading some 3rd party code and I found this: x.Flags = x.Flags
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I recently wanted use regex in Cocoa app. But I found that Cocoa does
I wanted to create some clickable PowerShell scripts, and I found this answer that
I've found this page about the Twitter search API and some operators : http://search.twitter.com/operators

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.