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

A

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
 
M

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
 
A

almurph

Mattias said:
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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top