I am pleased to announce the new release (v2023.6.2.2) of Spargine on February 8th, 2023, my open-source projects, and NuGet packages for .NET 6 & 7. I have added new classes, methods, benchmarks, and unit tests! This release features speed improvements based on the benchmark tests from the 3rd edition of “Rock Your Code: Code & App Performance for Microsoft .NET” available on Amazon. I use these in all the projects I work on, including many in production! I hope you will check them out and let me know what you would like added.
GitHub: https://github.com/RealDotNetDave/dotNetTips.Spargine/releases
NuGet: http://bit.ly/dotNetDaveNuGet
All the performance data for these assemblies can be found on GitHub. I am always looking for help with these projects, especially writing more unit tests. If you would like to help, please email me at dotnetdave@live.com.
FastStringBuilder
As part of this release, I changed StringBuilderHelper
to FastStringBuilder
since it now uses the StringBuilder from an ObjectPool.
CombineStrings()
This method converts an array of strings into a string. Line feeds can also be added after every string. Here is an example of how to use it.
var result = FastStringBuilder.CombineStrings(Tristate.False, strings);
Benchmark Results
Here are the benchmark results for comparing CombineStrings()
to the regular way of doing it with a StringBuilder
.
As you can see, CombineStrings()
is a lot faster. It’s 1.64 times faster in .NET 7 and 1.58 times faster in .NET 6!
PerformAction()
This method will allow developers to perform an action using StringBuilder
from the ObjectPool
. Here is an example of how to use it.
Action<StringBuilder> action = (StringBuilder sb) => { foreach (var word in strings) { _ = sb.Append($"WORD:{word}|"); } }; var result = FastStringBuilder.PerformAction(action);
Benchmark Results
These are the benchmark results that show the difference using the StringBuilder
from the ObjectPool
.
As you can see, PerformAction()
is faster. In .NET 7 it’s 1.67 times faster and 1.56 times faster in .NET 6!
BytesToString()
This method will convert a byte array to a string. Here is an example of how to use it.
var result = FastStringBuilder.BytesToString(byteArray);
ToDelimitedString()
This method will convert an Dictionary<,>
of text to a delimited string as shown below.
var result = FastStringBuilder.ToDelimitedString(wordDictionary);
By default, a comma is used to delimit the strings. Any character can also be used.
PerformanceStopwatch
A long time ago, I added the type PerformanceStopwatch
to make it easier to use StopWatch
. For this version of Spargine, I added new methods that also log a message using ILogger.
StartNew()
To use the PerformanceStopwatch
, call the StartNew()
method. This creates a PerformanceStopwatch
and then starts it.
StopReset()
This method stops the Stopwatch
, resets the elapsed time to zero, and returns the elapsed time. Here is an example of how to use it.
var psw = PerformanceStopwatch.StartNew(); // Perform work TimeSpan result = psw.StopReset();
StopRestart()
This method stops the Stopwatch
, resets the elapsed time to zero, restarts the time, and returns the elapsed time. Here is an example of how to use it.
var psw = PerformanceStopwatch.StartNew(); // Perform work TimeSpan result = psw.StopRestart();
Reset or Restart Plus Logging
Both the StopReset()
and StopRestart()
now supports logging when called. Here is an example of how to use it.
var psw = PerformanceStopwatch.StartNew(title: "LoadUsers()"); // Perform Work var result = psw.StopRestart(logger, message: "Call to database.");
These overloads add time to the end of the message as shown below.
Diagnostics
The Diagnostics
property will return a collection of all the logged messages. Here is an example of the output.
GetUsers():Load users from database. Time: 113.02 ms GetUsers():Save users to database. Time: 13.7925 ms
Calling ToString()
will return this information as a string with line feeds. I hope that you will use PerformanceStopwatch to make capturing and logging timing easy!
New Methods
Here are more new methods that have been added to this version.
ArrayExtensions
A few new methods were added to ArrayExtensions
.
PerformAction<T>()
This method will perform an Action
against an array and considers that reference, value, and record types perform differently, so it uses the fastest way to loop over an array based on the type. Here is an example of how to use it.
var sb = new StringBuilder(); people.PerformAction((person) => { sb.Append($"{person.ToString()}|"); }); var result = sb.ToString();
Other Methods
FastCount()
: Returns the count of an array as a long.FastProcessor<T>()
: Processes the collection with the specified action.
ListExtentions
PerformAction<T>()
: Performs an action for the items in a List.ToReadOnlyObservableCollection<T>()
: Converts a List to aReadOnlyObservableCollection<T>
.
StringExtensions
CombineToString()
: Combines a string using theFastStringBuilder.CombineStrings()
method.ToByteArray()
: Converts a string to a byte array.
Summary
I hope you will find these methods and more in Spargine useful for your projects. Benchmark results are published on GitHub and the links to them are in the ReadMe file. If you would like anything added, please do a pull request. If you have any comments or suggestions, please make them below.
You can pick up a copy of my new code performance book by using this link: https://bit.ly/CodePerf3.
Happy coding geeks!
One thought on “Coding Faster with dotNetTips.com Spargine 6: February 2023 Release”