473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String -> StringBuilder Optimization

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.
Sep 29 '06 #1
9 1902

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:uw**************@TK2MSFTNGP03.phx.gbl...
OK, I can't figure out a way to optimize the following VB 2005 code using
StringBuilders:
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?
Sep 29 '06 #2
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.

"Robinson" <it*****************@nowmyinboxisfull.comwrote in message
news:ef*******************@news.demon.co.uk...
>
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:uw**************@TK2MSFTNGP03.phx.gbl...
>OK, I can't figure out a way to optimize the following VB 2005 code using
StringBuilders:

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?

Sep 29 '06 #3

Michael D. Ober wrote:
OK, I can't figure out a way to optimize the following VB 2005 code using
StringBuilders:
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

Sep 29 '06 #4
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

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message news:uw**************@TK2MSFTNGP03.phx.gbl...
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.


Sep 30 '06 #5
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.nospamschreef in bericht
news:uw**************@TK2MSFTNGP03.phx.gbl...
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.


Sep 30 '06 #6
To Cor and all the other responders,

Thanks for all your imput. I'll leave it as is for now. I do have some
changes I'm thinking about to make the code itself easier to use, but the
basic structure will remain the same.

Mike Ober.

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:eu**************@TK2MSFTNGP03.phx.gbl...
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.nospamschreef in bericht
news:uw**************@TK2MSFTNGP03.phx.gbl...
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.




Sep 30 '06 #7

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:_e***************@newsread1.news.pas.earthlin k.net...
To Cor and all the other responders,

Thanks for all your imput. I'll leave it as is for now. I do have some
changes I'm thinking about to make the code itself easier to use, but the
basic structure will remain the same.

Mike Ober.
When I first came to .NET, I had trouble getting used to GC as I have a C++
background. Looking at all of the references created and the much larger
hit on memory really made me think I'd missed something important in my
implementation. It led me down the road of forcing the GC to collect
whenever I could. However, over time the stress hormone release from
watching the heap has decreased a great deal as my understanding of the CLR
has gone up. In your class above, I think you are already doing the best
you can without unintentionally obfuscating your code. Just don't look at
the heap so often - the GC will take care of all references no longer in
use, eventually.

Robin
Oct 2 '06 #8
Thanks - I was actually looking to see if anyone had ideas to reduce the
number of thrown away strings, which would in turn reduce the number of
allocations in the Gen(0) heap. Reducing these allocations would increase
the amount of time between runtime calls to the garbage collector, since
every allocation potentially involves a call to the garbage collector. From
my understanding of the system.string class, every single mid$() insert
statement will cause the internal string buffer to be thrown away and a new
one created (unlike in VB 6 where mid$() inserts replace part of the
existing string, but since the environment I'm working in is very heavily
string oriented, I couldn't see anyway to avoid this performance penalty,
which is why I asked.

As for explicitely calling the GC, I only do it when I need to get a picture
of how much memory is actually being used, and even then, I know it's only
an estimate. The GC in dotNet 2.0 appears to be one of the better ones on
the market, with only really long lived objects requiring true compaction
towards low memory. (On x86 processors, the heap almost always grows up
while the stack always grows down.) The GC of Gen(0) and Gen(1) memory is
nothing more than moving accessible gen(n) to the gen(n+1) heap and
adjusting the gen(n+1) heap free pointer, meaning that the GC doesn't have
to keep track of holes in memory until it gets to Gen(3). This makes Gen(0)
and Gen(1) compaction single pass, while keeping the full benefits of the
multi-pass Mark/Sweep/Compaction available to the runtime at the Gen(3) heap
level.

Mike Ober.

"Robinson" <it*****************@nowmyinboxisfull.comwrote in message
news:ef*******************@news.demon.co.uk...
>
"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:_e***************@newsread1.news.pas.earthlin k.net...
>To Cor and all the other responders,

Thanks for all your imput. I'll leave it as is for now. I do have some
changes I'm thinking about to make the code itself easier to use, but the
basic structure will remain the same.

Mike Ober.

When I first came to .NET, I had trouble getting used to GC as I have a
C++ background. Looking at all of the references created and the much
larger hit on memory really made me think I'd missed something important
in my implementation. It led me down the road of forcing the GC to
collect whenever I could. However, over time the stress hormone release
from watching the heap has decreased a great deal as my understanding of
the CLR has gone up. In your class above, I think you are already doing
the best you can without unintentionally obfuscating your code. Just
don't look at the heap so often - the GC will take care of all references
no longer in use, eventually.

Robin

Oct 2 '06 #9

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:Or**************@TK2MSFTNGP06.phx.gbl...
Thanks - I was actually looking to see if anyone had ideas to reduce the
number of thrown away strings, which would in turn reduce the number of
allocations in the Gen(0) heap. Reducing these allocations would increase
the amount of time between runtime calls to the garbage collector, since
every allocation potentially involves a call to the garbage collector.
From my understanding of the system.string class, every single mid$()
insert statement will cause the internal string buffer to be thrown away
and a new one created (unlike in VB 6 where mid$() inserts replace part of
the existing string, but since the environment I'm working in is very
heavily string oriented, I couldn't see anyway to avoid this performance
penalty, which is why I asked.
Yes, strings in .NET are immutable. I had a serious performance problem
with one of my methods that concatenated 10,000 or so "numbers" (string
numbers), with comma separators using normal string concatenation in my
project a while back. The stringbuilder gives an order of magnitude
performance increase in this case in terms of speed. I would however
optimize these special cases where a problem is identified but prefer
ordinary string manipulation otherwise.
Oct 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.