|
-
Jan 23rd, 2008, 09:01 AM
#1
Thread Starter
Frenzied Member
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?
-
Jan 23rd, 2008, 09:48 AM
#2
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.
-
Jan 23rd, 2008, 10:04 AM
#3
Thread Starter
Frenzied Member
Re: Which syntax is more efficient?
 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 Maybe someone with knowledge of the internals of the String.Compare() method will chime in 
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
-
Jan 23rd, 2008, 10:29 AM
#4
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
-
Jan 23rd, 2008, 01:52 PM
#5
Thread Starter
Frenzied Member
Re: Which syntax is more efficient?
I thought String.Equals() checked for reference equality.
-
Jan 23rd, 2008, 02:04 PM
#6
Re: Which syntax is more efficient?
It doesnt
Determines whether two String objects have the same value. /MSDN
-
Jan 23rd, 2008, 02:10 PM
#7
Re: Which syntax is more efficient?
http://www.aisto.com/roeder/dotnet/ - decompile and read the methods at will
-
Jan 23rd, 2008, 07:57 PM
#8
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());
-
Jan 23rd, 2008, 10:27 PM
#9
Thread Starter
Frenzied Member
Re: Which syntax is more efficient?
 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.
-
Jan 23rd, 2008, 10:39 PM
#10
Re: Which syntax is more efficient?
 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:
 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.
Last edited by jmcilhinney; Jan 23rd, 2008 at 10:47 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|