kamagra how much to take

String.Compare for Performance And Readability

Sometimes, I am just so used to doing things a certain way that I don’t look for new ways of doing it. Fault of my programming personality I suppose, but String.Compare is one of those things that I use religiously now. Let’s take a quick example.

We might go about retrieving a some SPUser objects from an SPGroup with a specific name.Name property.

To do this, we might use the following method, which takes a SPWeb object as a parameter, along with a string that represents the SPGroup Name property that we wish to match, and subsequently we will add the SPUser objects that are contained in the SPGroup object to our locally declared generic collection:

  1. private List<SPUser> RetrieveSomeUsersFromGroup(SPWeb oWeb, string strGroupName)
  2.  
  3. {
  4.  
  5. List<SPUser> l_gUserCollection = new List<SPUser>();
  6.  
  7. foreach (SPGroup l_oGroup in oWeb.SiteGroups)
  8.  
  9. {
  10.  
  11. if (l_oGroup.Name.ToLower() == strGroupName.ToLower())
  12.  
  13. {
  14.  
  15. foreach (SPUser l_oUser in l_oGroup.Users)
  16.  
  17. {
  18.  
  19. l_gUserCollection.Add(l_oUser);
  20.  
  21. }
  22.  
  23. }
  24.  
  25. }
  26.  
  27. return l_gUserCollection;
  28.  
  29. }

Yeah, there are better examples, but this was all I could whip up on a whim :)

The line I want to draw attention to is:

  1. if (group.Name.ToLower() == strGroupName.ToLower())

Yeah, this is normal. We are placing the lower case string representation of each string in order to handle case sensitivity. The strings that are created when you are doing your dev are immutable, and therefore each time you are going to be creating a new instance of that string when calling it, and as an easy rule of practice, fewer objects mean less memory consumption and ergo better speed of code execution.

Enter String.Compare. In order to re-write this method as such, we could both increase the readability of the code (IMHO) as well as introduce performance (I am only focusing on that one line :p ). In the method now, I am going to use String.Compare, and I am going to use Equals instead of the equality operator == (I never like using that).

  1. private List<SPUser> RetrieveSomeUsersFromGroup(SPWeb oWeb, string strGroupName)
  2.  
  3. {
  4.  
  5. List<SPUser> l_gUserCollection = new List<SPUser>();
  6.  
  7. foreach (SPGroup l_oGroup in oWeb.SiteGroups)
  8.  
  9. {
  10.  
  11. if (Equals(String.Compare(l_oGroup.Name, strGroupName, true), 0))
  12.  
  13. {
  14.  
  15. foreach (SPUser l_oUser in l_oGroup.Users)
  16.  
  17. {
  18.  
  19. l_gUserCollection.Add(l_oUser);
  20.  
  21. }
  22.  
  23. }
  24.  
  25. }
  26.  
  27. return l_gUserCollection;
  28.  
  29. }

That to me is a little cleaner, and also would consume less memory and increase performance (but hell, it may not be noticeable, best practice IMHO I deduce).

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>