C#
  Home arrow C# arrow Page 9 - Strings and Characters, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Strings and Characters, Part 2
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2004-08-03

    Table of Contents:
  • Strings and Characters, Part 2
  • 2.14 Passing a String to a Method that Accepts Only a Byte[ ]
  • 2.15 Converting Strings to Their Equivalent Value Type
  • 2.16 Formatting Data in Strings Problem
  • 2.17 Creating a Delimited String
  • 2.18 Extracting Items from a Delimited String
  • 2.19 Setting the Maximum Number of Characters a String Can Contain
  • 2.20 Iterating Over Each Character in a String
  • 2.21 Improving String Comparison Performance
  • 2.22 Improving StringBuilder Performance
  • 2.23 Pruning Characters from the Head and/or Tail of a String

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Strings and Characters, Part 2 - 2.21 Improving String Comparison Performance


    (Page 9 of 11 )

    Problem

    Your application consists of many strings that are compared frequently. You have been tasked with improving performance and making more efficient use of resources.

    Solution

    Use the intern pool to improve resource usage and, in turn, improve performance. The Intern and IsInterned instance methods of the string class allow you to use the intern pool. Use the following static methods to make use of the string intern pool:

    using System;
    using System.Text;

    public class InternedStrCls

    {
    public static void CreateInternedStr(char[] characters)
    {

    string NonInternedStr = new string(characters); String.Intern(NonInternedStr);
    }

    public static void CreateInternedStr(StringBuilder strBldr)
    {
    String.Intern(strBldr.ToString());
    }

    public static void CreateInternedStr(string str)
    {
    String.Intern(str);
    }

    public static void CreateInternedStr(string[] strArray)

    {
    foreach(string s in strArray)

    {

    String.Intern(s);
    }
    }
    }

    Discussion

    The CLR automatically stores all string literals declared in an application in an area of memory called the intern pool. The intern pool contains a unique instance of each string literal found in your code, which allows for more efficient use of resources by not storing multiple copies of strings that contain the same string literal. Another benefit is speed. When two strings are compared using either the == operator or the Equals instance method of the string class, a test is done to determine whether each string variable reference is the same;if they are not, then each string’s length is checked;if both string’s lengths are equal, each character is compared individually. However, if we could guarantee that the references, instead of the string contents, could be compared, much faster string comparisons can be made. String interning does just that: it guarantees that the references to equivalent string values are the same, eliminating the possibility of attempting the length and character-by-character checks. This yields better performance in situations where the references to two equal strings are different and the length and character-by-character comparisons have to be made.

    Note that the only strings automatically placed in this intern pool by the compiler are string literals—strings surrounded by double quotes—found in code by the compiler. The following lines of code will place the string "foo" into the intern pool:

    string s = "foo";

    StringBuilder sb = new StringBuilder("foo");

    StringBuilder sb = new StringBuilder().Append("foo");

    The following lines of code will not place the string "foo" into the intern pool:

    char[] ca = new char[3] {'f','o','o'};

    StringBuilder sb = new StringBuilder().Append("f").Append("oo");

    string s1 = "f"; string s2 = "oo"; string s3 = s1 + s2;

    You can programmatically store a new string created by your application in the intern pool using the static string.Intern method. This method returns a string referencing the string literal contained in the intern pool, or, if the string is not found, the string is entered into the intern pool and a reference to this newly pooled string is returned.

    There is also another method used in string interning called IsInterned. This method operates similarly to the Intern method, except that it returns null if the string is not in the intern pool, rather than adding it to the pool. This method returns a string referencing the string literal contained in the intern pool, or, if the string is not found, it returns null.

    An example of using this method is shown here:

    string s1 = "f";
    string s2 = "oo";
    string s3 = s1 + s2;
    if (String.IsInterned(s3) == null)
    {

    Console.WriteLine("NULL");
    }

    However, if we add the highlighted line of code, the IsInterned test returns a non-null string object:

    string s1 = "f";

    string s2 = "oo";
    string s3 = s1 + s2;

    InternedStrCls.CreateInternedStr(s3);

    if (String.IsInterned(s3) == null) { Console.WriteLine("NULL"); }

    The Intern method is useful when you need a reference to a string, even if it does not exist in the intern pool.

    The IsInterned method can optimize the comparison of a single string to any string literal or manually interned string. Consider that you need to determine whether a string variable contains any string literal that has been defined in the application. Call the string.IsInterned method with the string variable as the parameter. If null is returned, there is no match in the intern pool, and thus there is no match between the string variable’s value and any string literals:

    string s1 = "f";
    string s2 = "oo";
    string s3 = s1 + s2;

    if (String.IsInterned(s3) != null)

    {

    // If the string "foo" has been defined in the app and placed

    // into the intern pool, this block of code executes.

    }
    else
    {

    // If the string "foo" has NOT been defined in the app NOR been placed
    // into the intern pool, this block of code executes.
    }

    Exercise caution when using the string interning methods. Calling the Intern method for every possible string that could be created by your application would actually cause the application’s performance to slow considerably, since this method must search the intern pool for the string;if it does not exist in the pool, it is added. The reference to the newly created string in the intern pool is then returned.

    Another potential problem with the IsInterned method in particular stems from the fact that every string literal in the application is stored in this intern pool at the start of the application. If you are using IsInterned to determine whether a string exists, you are comparing that string against all string literals that exist in the application, as well as any you might have explicitly interned, not just the ones in the scope in which IsInterned is used.

    See Also

    See the “String.Intern Method” and “String.IsInterned Method” topics in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


       · The encoder no longer accepts byte arrays only ubyte arrays so this information is...
     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway