views:

194

answers:

3

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  A: 

I believe TestOne will be faster because in TestTwo you are creating a new Regex object every time you loop. If FindDays is implemented the same as FindNumber it will be even worse as you'll be creating two objects.

Jon Freeland
+1  A: 

Technically, TestOne will be faster, because TestTwo is adding a stack frame by calling FindNumber().

I don't know how much difference it will make, I doubt it's that much. Your method is static, so really it's just creating the object, which should be pretty fast.

My question to you is why are you using a function call to return the same string over and over? Why don't you just declare a real variable?

Like,

private static Regex _findNumber = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
womp
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.
Scott
Every time the class is accessed? Absolutely not. Every time the class is created, sure - you'd create one private variable for it. But just dereferencing a variable is going to be faster than calling a function to do exactly the same thing.
womp
womp, I would then have about 100 private variables in one class due to its size, but on any instance, only about 3 or 4 or those variables are used each time I created a class. I am a web based programmer, so as far as I know classes are recreated every time a postback is made and a method inside the class is called. Am I correct?So Since I only use 3 or 4 of those newly created variables each time the class is 'created' it would be better for me to put each one into a method. Is that correct?
Scott
Not if you make it static. Anything that is static is created once. In fact, if you create a static variable in MyClass, you can always call MyClass.MyVariable without ever even creating an instance of MyClass. It would be created once, exactly like the current function you are currently using, except that you wouldn't have to call a function. You can read up on it here: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
womp
+4  A: 

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.

Lasse V. Karlsen
I am going to repeat this comment as stated on womps answer, because it still applies.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.
Scott
Scott: The code provided above creates the Regex stored in _FindNumber the first time the FindNumber static property is accessed and it will continue to exist until the program ends.
R. Bemrose
Every time you call the method that returns a new Regex object, you'll get a new Regex object, which will take time to construct, and add GC pressure. If you cache that object so that the next time you call that same function, it just fetches the previously generated object, you'll save time, and GC pressure. If you call the function, and it doesn't cache, in a loop, each loop iteration will take more time since it constructs more objects, and add more GC pressure. Does that answer your question? If not, be specific.
Lasse V. Karlsen
Being a bit more specific, I am a web based programmer in ASP.Net. So when I say that it can get run many times, it will be after each page refresh and not in the same "context" So that means that during each server call, a new thread is created and then destroyed. There is no way to keep that object alive between each server call unless I cache it into Session. And right now, I don't think re factoring that much will speed it up.Anything else I can do to stop the method from recreating it self?
Scott
Note that a static field will keep its value for the entire web application, which means across different users, and across sessions.
Lasse V. Karlsen
Thank you for that very last comment. That sure does clear up a lot of things and makes me want to rethink a bit of my code.
Scott