473,804 Members | 3,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1936

"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
news:uw******** ******@TK2MSFTN GP03.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************ *****@nowmyinbo xisfull.comwrot e in message
news:ef******** ***********@new s.demon.co.uk.. .
>
"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
news:uw******** ******@TK2MSFTN GP03.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.BlockCop y. 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 StringDictionar y 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 StringDictionar y and simply get/set them when required.

HTH

--
Dave Sexton

"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message news:uw******** ******@TK2MSFTN GP03.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.nospams chreef in bericht
news:uw******** ******@TK2MSFTN GP03.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******** ******@TK2MSFTN GP03.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.nospams chreef in bericht
news:uw******** ******@TK2MSFTN GP03.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.nospamw rote in message
news:_e******** *******@newsrea d1.news.pas.ear thlink.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************ *****@nowmyinbo xisfull.comwrot e in message
news:ef******** ***********@new s.demon.co.uk.. .
>
"Michael D. Ober" <obermd.@.alum. mit.edu.nospamw rote in message
news:_e******** *******@newsrea d1.news.pas.ear thlink.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.nospamw rote in message
news:Or******** ******@TK2MSFTN GP06.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
6757
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... this number is always changing as I have hundreds of thousand of pages of text on my site. Problem: - in my opinion this just not only look weird, but the variable "n" (number)
5
31184
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 will split myString up using the delimiter of 1 space so that
9
8006
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
9
3700
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
10
8198
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
2
4787
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
4327
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
2
3181
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 a tasty burger"
11
3076
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 spaces, but not in the inner string(\"and a inner string\\\"). Will anyone please tell me how to do this?
8
4745
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 add the unique words in the string to a map. Eg:
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.