Power of StringBuilder Class

Power of StringBuilder Class

What is StringBuilder class? We had this question few years back. We googled it for QTP implementation and found the below article from AdvancedQTP (Our Favorite Site).

VB and VBScript are slow with string operations because each string is stored as a block of memory. If a string length is increased, then it requires a new block of memory and the existing and new content will be accommodated into the new block.

We were stumbled when we read it because the value which is held by StringBuilder can be modified without any new memory allocation. For better understanding of this, we should know where VBScript String operation lacks.

VB and VBScript are slow with string operations because each string is stored as a block of memory. If a string length is increased, then it requires a new block of memory and the existing and new content will be accommodated into the new block.

The problem is: If we increase the string size often or use looping string concatenation,then we will see the performance decrease here. The reason is it does reallocation whenever the string size increases. In QTP script development, most of the time we work with strings. For example: Populating result into HTML file, Reading strings from file and handling the test data, etc.

Whenever we perform a loop string concatenation or strings are concatenated more number of times arbitrarily, we can use StringBuilder class.

Code


Synopsis

We can see increase in performance whenever we handle a large number of string operations using StringBuilder class.

References: MSDN

Leave a Reply

Your email address will not be published. Required fields are marked *