Results 1 to 10 of 10

Thread: Which syntax is more efficient?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

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

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    Re: Which syntax is more efficient?

    I thought String.Equals() checked for reference equality.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Which syntax is more efficient?

    It doesnt

    Determines whether two String objects have the same value. /MSDN
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Which syntax is more efficient?

    http://www.aisto.com/roeder/dotnet/ - decompile and read the methods at will

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Which syntax is more efficient?

    If you want to know which one's faster then test for yourself:
    CSharp Code:
    1. string str = "ViewCollection";
    2. Stopwatch timer;
    3.  
    4. timer = Stopwatch.StartNew();
    5.  
    6. for (int i = 0; i < 100000; i++)
    7. {
    8.     if (string.Compare(str, "viewcollection", true) == 0)
    9.     {
    10.     }
    11. }
    12.  
    13. MessageBox.Show(timer.Elapsed.ToString());
    14.  
    15. timer = Stopwatch.StartNew();
    16.  
    17. for (int i = 0; i < 100000; i++)
    18. {
    19.     if (str.ToLower() == "viewcollection")
    20.     {
    21.     }
    22. }
    23.  
    24. MessageBox.Show(timer.Elapsed.ToString());
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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:
    1. string str = "ViewCollection";
    2. Stopwatch timer;
    3.  
    4. timer = Stopwatch.StartNew();
    5.  
    6. for (int i = 0; i < 100000; i++)
    7. {
    8.     if (string.Compare(str, "viewcollection", true) == 0)
    9.     {
    10.     }
    11. }
    12.  
    13. MessageBox.Show(timer.Elapsed.ToString());
    14.  
    15. timer = Stopwatch.StartNew();
    16.  
    17. for (int i = 0; i < 100000; i++)
    18. {
    19.     if (str.ToLower() == "viewcollection")
    20.     {
    21.     }
    22. }
    23.  
    24. 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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Last edited by jmcilhinney; Jan 23rd, 2008 at 10:47 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width