Optimizing String Processing in Collections: The Impact of PerformAction() in Spargine and FastStringBuilder

Some time ago, I introduced a method in Spargine named PerformAction() designed to enhance the efficiency of processing data within collections. Subsequently, I integrated this method into FastStringBuilder with the aim of optimizing the speed of processing strings.

The conventional approach developers employ to concatenate a collection of words into a single string typically involves utilizing a StringBuilder:

var sb = new StringBuilder();

for (var index = 0; index < this._words.Length; index++)
{
    _ = sb.Append(this._words[index]);
}

return sb.ToString();

However, we can significantly expedite this process by leveraging PerformAction(), as illustrated below:

Action<StringBuilder> action = (StringBuilder sb) =>
{
    for (var index = 0; index < this._words.Length; index++)
    {
        _ = sb.Append(this._words[index]);
    }
};

return FastStringBuilder.PerformAction(action);

Benchmark Results

The results clearly demonstrate that using PerformAction() yields a performance increase of 1.79 times while also reducing memory allocation.

Allocations: Normal: 272 – 13,984 bytes, PerformAction(): 128 – 5,208 bytes.

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.