Groups | Blog | Home
all groups > dotnet framework > september 2006 >

dotnet framework : String -> StringBuilder Optimization



Michael D. Ober
9/29/2006 9:52:06 AM
OK, I can't figure out a way to optimize the following VB 2005 code using
StringBuilders:

Public Const RecSize as Integer = 105
Private buffer As String

Public Sub New()
init
End Sub
Public Sub New(ByVal value As String)
buffer = LSet(value, 105)
End Sub

Public Sub init()
buffer = Space(105)
End Sub

Private Function fetch(ByVal start As Integer, ByVal chars As Integer) As
String
fetch = Mid(buffer, start, chars)
End Function
Private Sub putch(ByRef s As String, ByVal start As Integer, ByVal chars As
Integer)
Mid(buffer, start, chars) = LSet(s, chars)
End Sub

Public Property BUF() as String
Get
Return fetch(1, 105)
End Get
Set(ByVal value as string)
putch(value, 1, 105)
End Set
End Property

Public Property KEY0() as String
Get
Return fetch(1, 19)
End Get
Set(ByVal value as string)
putch(value, 1, 19)
End Set
End Property

' Additional fields are also defined in the same manner.

The public interface to this class must remain as strings, but I'd like to
optimize the implementation. Note that this is a sample from our interface
to a VMS BASIC environment and this code is actually being generated by a VB
6 program from the VMS Record Management Services (RMS) definition files.
Once a data record is in this object, I need to be able to change it, but
don't want to drive the GC nuts allocating/deallocating the internal buffer
string. For each class, the total length of buffer is fixed. I can't use a
simple record because RMS allows overlapping variants and the VB 6 program
has been coded to support that.

Mike Ober.

Michael D. Ober
9/29/2006 1:20:44 PM
So far, no performance issues, but it's used a lot - as in every single
field in our database runs through this type of code. Some of the tables
have over 100 fields (I didn't design it - I just have to live with it).

Mike.

[quoted text, click to view]

Tom Shelton
9/29/2006 2:48:11 PM

[quoted text, click to view]

I don't think I would use a StringBuilder here. Maybe just a plain old
char array along with Buffer.BlockCopy. Strings could still be
returned when needed - just pass a char array to the string
constructor.

--
Tom Shelton
Robinson
9/29/2006 5:40:51 PM

[quoted text, click to view]

Can I ask a simple question? Are you experiencing performance problems with
this piece of code? Is it slow, or eating up all of your available memory
or something nasty like that?

Dave Sexton
9/29/2006 9:49:36 PM
Hi Michael,

If you haven't experienced any issues then you might not want to concern yourself with modifying code that works, as I believe
Robinson was suggesting by asking questions related to the performance of your design, but I do understand if you only want to
perform some simple optimizations ahead of time.

That said, have you thought about using a StringDictionary instead of a StringBuilder or string buffer?

You might want to just parse the complete input string once, or parse each field on-demand and store the references to each of the
fields' values in case they are recalled at a later time. If you can design the class so that the parsing of each field is only
done once then a StringBuilder would be of no use, unless of course you need to rebuild the buffer for output with new values.

It might be cheaper to simply rebuild the buffer on-demand then it would be to perform string manipulation in each properties'
get/set accessor, but that of course depends on the number of property read/writes compared to the number of times your class will
need to rebuild the buffer for output and the actual tasks performed by each operation. I think, without those statistics, that
parsing in the get/set accessors each time will probably have an increased negative effect on your application's performance and the
GC more than if you were to cache the fields values in a StringDictionary and simply get/set them when required.

HTH

--
Dave Sexton

[quoted text, click to view]

Cor Ligthert [MVP]
9/30/2006 12:00:00 AM
Michael,

I think using something else than as the code is now, will influence the
performance. It will in my idea go slower.

Cor

"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> schreef in bericht
news:uwE14894GHA.4196@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button