Which syntax is more efficient?
I'm trying to optimize a few methods while I'm converting the code from VB.net ( version 1.1 ) to c# ( version 2.0 )
I found a lot of these statements to compare strings :
Code:
If String.Compare(e.CommandName, "viewcollection", True) = 0 Then
I have chosen to rewrite them as simply :
Code:
if(e.CommandName.ToLower() == "viewcollection")
is either way any more efficient than the other?
Normally, this wouldn't be such a big deal but I found hundreds of these statements and want to minimize overhead as much as possible.
Statement 1 uses a static(shared) method of the string class and statement 2 simply does a comprison. Does statement 2 create a copy of the string and therefore creates more overhead?
Re: Which syntax is more efficient?
I would say that the first one is more efficient. The second one does create a new string with all lower-case letters, before doing the actual comparison.
I dont know how the string.Compare method works internally when comparing non-case sensitive, but I think its pretty safe to say that it does it more effective than #2.
Re: Which syntax is more efficient?
Quote:
Originally Posted by Atheist
I would say that the first one is more efficient. The second one does create a new string with all lower-case letters, before doing the actual comparison.
I dont know how the string.Compare method works internally when comparing non-case sensitive, but I think its pretty safe to say that it does it more effective than #2.
I'm willing to bet you are right on the money :D Maybe someone with knowledge of the internals of the String.Compare() method will chime in :D
Why am I stressing over this? Cause this website gets over one thousand hits a day and I am trying to optimize some bottlenecks we've found :(
Re: Which syntax is more efficient?
String.Equals is probably faster than String.Compare by the way. Also, heres an interesting article I found:
http://blogs.msdn.com/abhinaba/archi...07/510169.aspx
Re: Which syntax is more efficient?
I thought String.Equals() checked for reference equality.
Re: Which syntax is more efficient?
It doesnt;)
Determines whether two String objects have the same value. /MSDN
Re: Which syntax is more efficient?
http://www.aisto.com/roeder/dotnet/ - decompile and read the methods at will :)
Re: Which syntax is more efficient?
If you want to know which one's faster then test for yourself:
CSharp Code:
string str = "ViewCollection";
Stopwatch timer;
timer = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
if (string.Compare(str, "viewcollection", true) == 0)
{
}
}
MessageBox.Show(timer.Elapsed.ToString());
timer = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
if (str.ToLower() == "viewcollection")
{
}
}
MessageBox.Show(timer.Elapsed.ToString());
Re: Which syntax is more efficient?
Quote:
Originally Posted by jmcilhinney
If you want to know which one's faster then test for yourself:
CSharp Code:
string str = "ViewCollection";
Stopwatch timer;
timer = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
if (string.Compare(str, "viewcollection", true) == 0)
{
}
}
MessageBox.Show(timer.Elapsed.ToString());
timer = Stopwatch.StartNew();
for (int i = 0; i < 100000; i++)
{
if (str.ToLower() == "viewcollection")
{
}
}
MessageBox.Show(timer.Elapsed.ToString());
Thanks "j" Speed IS a concern but I was worried more about how much overhead is being piled up on the server in this situation.
Re: Which syntax is more efficient?
Quote:
Originally Posted by Andy
Does statement 2 create a copy of the string and therefore creates more overhead?
The MSDN documentation for the String.ToLower method describes it thusly:
Quote:
Originally Posted by MSDN
Returns a copy of this String converted to lowercase.
I guess you must have overlooked that part when you read it.
That said, the String object created is immediately discarded so it's immediately available for garbage collection. What's "overhead" when the memory occupied by the String objects created is immediately able to be reclaimed? The issue is the time it takes to create those objects, which is why that way is slower.