a view of the Marsh

...: Chatter by Year

2020

July

2016

October

2014

March

February

2013

September

July

June

March

February

January

2012

December

November

October

September

August

July

June

May

April

March

February

January

2011

November

October

July

June

May

April

March

February

January

2010

December

November

October

September

August

July

June

May

April

March

February

January

2009

December

November

October

September

August

July

June

May

April

March

February

January

2008

December

November

October

September

August

...: Chatter by Keyword

ASP.NET

Browsers

Business

Cascading Styles (CSS)

Charity Work

Community

Definitions

Design

Education

Ethics

Firefox

First Post

Hockey

I.T. Events

Internet Explorer

Junk mailer/poster

Life

Microsoft

MS Access

Music

Other sports

Printers

Programming

sci-fi

Software

Speaking/Presentations

SQL

Survey

Teaching

The Marsh

Tips & Tricks

Tools of Trade

U.S. Events

Visual Basic (VB.NET)

Web Design

World Events

Ziva

...: Marsh Chatter

Choices… A Recurring Series: StringBuilder…

String concatenation (e.g. a fancy word for joining strings) is one of the most frequent processes a program performs. Unfortunately, done wrong it can be the major cause of a slow running program – especially when the concatenation is buried in a loop.

Little Background? What is this Immutable thing?

Don’t you just love terminology? Oh so necessary, but the hard part of learning. In short, the common dictionary definition for immutable is something that doesn’t change.

In our industry, strings are known to be immutable; that is, if you change a string, your program actually creates a new one and then deletes the original. There is a cost in time and memory for this process. On top of that – strings are one of the most memory intensive data types we have. Hence, this type of string operation tends to be slow.

Help is in the Framework

Within the .NET Framework is the StringBuilder object which enables fast string handling. Let me repeat that... fast string handling. If you've used it - you know! If you haven't, it is time to learn!

Testing, Examples, and Results on the web

Each of these articles offer great detail, suggestions, and results. There is no reason for you or I to duplicate their testing methods - unless of course you are just looking for a challenge.

Conclusions

Once you see the results in your own code, you might as a general rule, for consistency - argue for using the StringBuilder objects for any string concatenation. Of course, there are always caveats to any "rule"; such as, the slight cost in memory or processor cycles in setting up the object prior to usage.

If you're tacking 2 or 3 strings together, you probably aren't going to save much time, however, if your code will concatenate more or a variable number of strings together, you will probably want to use a StringBuilder to perform that work. This is especially true if the concat will occur within a loop.

Future - Choices… A Recurring Series:

While string concatenation is slow, your code probably has bigger optimization targets. One of the most common is accessing data in files (e.g. hard drive speed issue), or accessing data in files across a network (e.g. significant bandwidth issues).

We will look at potential solutions when using data files (e.g. file-based databases) in your programs. Specifically, this series will relate to web applications. See you then.