Home All Groups Group Topic Archive Search About

Intern Strings - am I using them right? Please help!

Author
23 Jan 2006 12:37 PM
almurph
Intern Strings - am I usingthem right.


    I have heard a lot about intern string - so I wanted to use them to
increas e speed of processing.

    I have a hastable that I am using to parse a string of the form:

wordA wordB wordC wordD etc etc

    I want to de-duplicate the string - that is, remove any repeated words
so that only 1 occurance of the word exists.

    Its working fine - takes about 25 mins to do all the strings that I
have for it so I intriduced the line:

String.Intern(s)

    in an effort in incrase spped of processing. My question is though -
am I using intern string correctly? Am I msiing anything or using them
incorrectly? Any comments/suggestions/code-samples much appreciated.

Al.



******** CODE AS FOLLOWS ********


    Public Function DeDuplicate(ByVal str As String) As String
        Dim ht As New Hashtable

        Dim s As String
        For Each s In str.Split(" "c)
            Try
                String.Intern(s)
                If Not ht.ContainsKey(s) Then
                    ht.Add(s, s)
                End If
            Catch ex As Exception
                Debug.WriteLine(ex.ToString)
            End Try
        Next

        'Convert hashtable -> string
        Dim deDupStr As String
        Dim Item As DictionaryEntry
        For Each Item In ht
            'String.Intern(deDupStr)
            'Trace.WriteLine(Item.Value)
            deDupStr += Item.Value.ToString + " "
        Next

        Return deDupStr.Trim()

    End Function

Author
23 Jan 2006 5:23 PM
Mattias Sjögren
>    I have heard a lot about intern string - so I wanted to use them to
>increas e speed of processing.

I think you could gain more by using a StringBuilder when recreating
the stirng instead of the += operator.


>String.Intern(s)
>
>    in an effort in incrase spped of processing. My question is though -
>am I using intern string correctly?

Why do you think interning ths string would make your processing
faster? Did you see any performance increase?


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
24 Jan 2006 4:44 PM
almurph
Mattias Sjögren wrote:
> >    I have heard a lot about intern string - so I wanted to use them to
> >increas e speed of processing.
>
> I think you could gain more by using a StringBuilder when recreating
> the stirng instead of the += operator.

Thanks. I'll try this and see does it imporve speed of processing.

> >String.Intern(s)
> >
> >    in an effort in incrase spped of processing. My question is though -
> >am I using intern string correctly?
>
> Why do you think interning ths string would make your processing
> faster? Did you see any performance increase?

No - afraid not. Nothing amazing to make me sit up.

Hope I used them right...

Thank for your help Mattias.
A.