5000x Faster String Concatenation #csharp #dotnet #coding
If you're building strings inside a loop using the + or += operator, you are destroying your application's performance. In C#, strings are immutable, which means every time you "add" to a string, you're not actually modifying it. Instead, you're creating a completely new string in memory and throwing the old one away. In a loop with thousands of iterations, this generates an enormous amount of garbage and puts immense pressure on memory, slowing everything down.
In a test with 100,000 items, this classic mistake took over 5 seconds to complete. The solution? Use the StringBuilder class. It is specifically designed for this scenario. Instead of creating new strings, StringBuilder manages an internal, mutable buffer. When we switched the test to use StringBuilder, the exact same operation took just 1 millisecond. That's over 5,000 times faster! For any non-trivial string building in a loop, StringBuilder isn't just an option; it's a necessity for high-performance code.
Видео 5000x Faster String Concatenation #csharp #dotnet #coding канала Ed Andersen
In a test with 100,000 items, this classic mistake took over 5 seconds to complete. The solution? Use the StringBuilder class. It is specifically designed for this scenario. Instead of creating new strings, StringBuilder manages an internal, mutable buffer. When we switched the test to use StringBuilder, the exact same operation took just 1 millisecond. That's over 5,000 times faster! For any non-trivial string building in a loop, StringBuilder isn't just an option; it's a necessity for high-performance code.
Видео 5000x Faster String Concatenation #csharp #dotnet #coding канала Ed Andersen
Комментарии отсутствуют
Информация о видео
30 июня 2025 г. 18:00:27
00:01:50
Другие видео канала