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
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf 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.