Difference between local variable and method called variable? FROM#
I would like to know which is faster. Help me.
I have a variable declared in a method like:
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 this:
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;
}
Is TestOne faster now or 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 they are curious because TestOne can get a significant share in my code, so I would like to know what would be better to implement.
Thanks guys.
** Edit: ** The code I'm using has an extremely large class. Its a text parser for text strategy. I'm trying to refactor it a bit and here's what I'm interested in here. If I create a private variable for the Regex, won't it get executed every time the class is accessed? This is my question for you.
a source to share
TestOne
will be faster than TestTwo
that because you are not creating a new regex for each iteration of the loop.
This has two advantages:
- The time used to parse and construct objects for the regex is only executed once, not
mc.Count
times - Less pressure on garbage collection as fewer objects are built.
However, I would go one step further. If you will always return the same regex and you are concerned about speed, I would cache this regex object in a static field.
For example, you can think about this:
private static Regex _FindNumber;
public static Regex FindNumber()
{
if (_FindNumber == null)
_FindNumber = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
return _FindNumber;
}
This will only create one object, shared, and save it.
However, here's my real answer.
To find out which one will be the fastest, you will need to measure your code, optionally with my variant thrown for a good estimate, and then decide. Never decide to optimize without hard data, you may end up wasting time rewriting code that can introduce new bugs that will require fixing, which you will spend more time on, only to take 1% more performance out of it.
Large optimizations are performed algorithmically, like changing the type of the sorting algorithm, and then only after that, if necessary, you move on to local optimizations, for example, tuning a loop.
Having said that, I would at least avoid constructing the object in a loop, this is just common sense.
a source to share
Technically TestOne will be faster because TestTwo adds a stack frame by calling FindNumber ().
I don't know what the difference is, I doubt it. Your method is static, so it just creates an object, which should be pretty fast.
My question to you is, why are you using a function call to repeat the same line over and over? Why don't you just declare a real variable?
how
private static Regex _findNumber = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
a source to share