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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:18:41+00:00 2026-05-11T20:18:41+00:00

I would like to know whats faster. Help me out. I have a variable

  • 0

I would like to know whats faster. Help me out.

I have a variable declared in a Method like so:

    public static Regex FindNumber()
{ return new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled); }

As you can see it returns a Regular Expression.

I also have another Method that looks like so:

    private static string TestOne(string RawData)
{
    Regex rgxFindNumber = FindNumber();
    Regex rgxFindDays = FindDays();
    for (int i = 0; i < mc.Count; i++)
    {
        int days = Convert.ToInt32(rgxFindNumber.Match(rgxFindDays.Match(mc[i].Value).Value).Value);
    }
    return RawData;
}

Now is the TestOne Method going to be faster or is TestTwo?

        private static string TestTwo(string RawData)
{
    for (int i = 0; i < mc.Count; i++)
    {
        int days = Convert.ToInt32(FindNumber().Match( FindDays().Match(mc[i].Value).Value).Value);
    }
    return RawData;
}

Now im curious because TestOne Can get called a aweful lot in my code so I would like to know what would be better to implement.

Thanks guys.

**Edit:**The code I am using has an extremely large class. Its a text parser for a text based strategy game. I am trying to refactor it a bit and thats what I am wondering here. If I do create a private variable for the Regex, wouldn’t it be run every time the class is accessed? Thats my question to you.

  • 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-11T20:18:42+00:00Added an answer on May 11, 2026 at 8:18 pm

    TestOne will be faster than TestTwo, because you’re not creating a new regular expression for every loop iteration.

    This has two benefits:

    • The time used to parse and construct the objects for the regex is only done once, instead of mc.Count times
    • Less pressure on garbage collection since fewer objects are constructed.

    However, I would go one step further. If you’re always going to return that same regular expression, and you’re concerned about speed, I would cache that regex object in a static field.

    For instance, you might consider this:

    private static Regex _FindNumber;
    public static Regex FindNumber()
    {
        if (_FindNumber == null)
            _FindNumber = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        return _FindNumber;
    }
    

    This would create just one object, total, and keep it around.

    However, and here’s my real answer.

    To actually know which one is going to be the fastest, you’re going to have to measure your code, optionally with my variant thrown in for good measure, and then decide. Never decide on optimizations without hard data, you might end up spending time rewriting code, which can introduce new bugs, which will need fixing, which you will spend more time on, only to eek out another 1% of performance.

    The big optimizations are done algorithmically, like changing the type of sorting algorithm, and then only afterwards, if necessary, you move on to local optimizations like loop tuning.

    Having said that, I would at least avoid constructing the object in the loop, that’s just common sense.

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

Sidebar

Related Questions

I would like to know what of the many XSLT engines out there works
I would like to know what is the difference between initializing a static member
I would like to know what semaphores, messageQueues, etc... are active in my vxWorks
I would like to know what kind of tool you use for writing your
I would like to know what's the best technique to do single sign-on in
I would like to know what would be the best way to do unit
I would like to know what are your practices when you test your classes.
I would like to know what the pros and cons are for using an
I would like to know what would be the impact of modifying web.config file
I would like to know what a Unit test and Web Test is in

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.