The article compares two methods of checking for the presence of a specific character in a string in C#. Benchmark results reveal that using a character in the search is 3.57 times more efficient than using a string, and Contains() outperforms StringComparison.Ordinal and StringComparison.OrdinalIgnoreCase in terms of speed.
Tag: Strings
Code It Any Way You Want: Constants vs. Read-only Fields
The article discusses best practices for checking strings for null in coding. It outlines three common methods: using == null, is null, or string.IsNullOrEmpty().
Mastering Globalization
Internationalization and localization are crucial for software projects, involving adapting to various languages, regions, and technical requirements. Delaying consideration until later stages can lead to significant costs and effort. The importance of addressing globalization from the outset is emphasized, along with a recommendation to explore the book "Rock Your Code: Coding Standards for Microsoft .NET" for comprehensive insights.
Optimizing String Performance
These articles delve into the intricacies of string handling in programming, emphasizing the fundamental role strings play in representing text. It explores various aspects such as checking for empty strings efficiently, verifying if an object is a string, string comparison methods, formatting strings, checking for substrings and characters, retrieving and slicing substrings, concatenating strings, optimizing concatenation with StringBuilder and ObjectPool, appending single characters, encoding and decoding strings, and string compression using Brotli, Deflate, GZip, and ZLib formats. Benchmark results are provided throughout, offering insights into performance considerations. Additionally, the chapter highlights recommendations and best practices for string manipulation while referencing my open-source project, Spargine, for streamlined string handling processes.
Code It Any Way You Want: Optimal Parameter Passing – Array vs. Params Keyword
The article explores the performance differences between passing parameters as arrays or using the params keyword in C#. Despite similarities in speed, the author recommends using the params keyword for its ease of use during function calls.
String Performance: Appending a Character using the StringBuilder
The use of a single character with a StringBuilder from an ObjectPool can improve performance. Benchmark results show similar overall performance, but without an ObjectPool, using a character becomes more significant. It is recommended to use a character in such cases. EditorConfig setup can check for this issue using dotnet_diagnostic.CA1834.severity = warning.
Code It Any Way You Want: Checking Strings for Null
The article discusses best practices for checking strings for null in coding. It outlines three common methods: using == null, is null, or string.IsNullOrEmpty().
String Performance: Optimizing String Substring Extraction – Slicing vs. AsSpan()
The content presents an alternative approach to obtaining a substring using slicing with ReadOnlySpan and AsSpan() methods, delivering performance improvement of 2.4 times based on benchmark tests. Configuration for detecting this issue in EditorConfig is also provided using dotnet_diagnostic.CA1831.severity = error.
Boosting Performance and Memory Efficiency: Introducing ToDelimitedString() with Source Generators in Spargine
The article introduces a method called ToDelimitedString() in programming, using a StringBuilder and commas to create a delimited string. Benchmark results indicate that this method is 1.23 times more performant and allocates less memory, thanks to the utilization of a source generator in Spargine.
String Performance: Checking for a Character
Developers commonly check for a specific character in a string using methods like "Contains('M')" or "Contains("M")". Benchmark results show that using a character is 3.57 times more efficient than using a string. To check for this issue, the configuration in EditorConfig is set to dotnet_diagnostic.CA1847.severity = error.
You must be logged in to post a comment.