{"id":6,"date":"2025-02-06T09:08:41","date_gmt":"2025-02-06T09:08:41","guid":{"rendered":"https:\/\/davidlindon.com\/?p=6"},"modified":"2025-02-06T09:08:41","modified_gmt":"2025-02-06T09:08:41","slug":"should-we-always-use-stringbuilder-in-c","status":"publish","type":"post","link":"https:\/\/davidlindon.com\/?p=6","title":{"rendered":"Should we always use StringBuilder in C#"},"content":{"rendered":"\n<p>I&#8217;ve seen developers use StringBuilder to concatenate 2 small strings in C# and I started thinking, are we so used to reaching for StringBuilder that we never stop and think, is this overkill? So I wanted to run some benchmarks and find the sweat spot in terms of speed and memory allocation where it makes sense to use StringBuilder instead of simple concatenation.<\/p>\n\n\n\n<p>Lets start off with a very simple example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"568\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084617.png\" alt=\"\" class=\"wp-image-8\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084617.png 712w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084617-300x239.png 300w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/figure>\n\n\n\n<p>Now ofcourse this is only doing one string operation by combining the 4 individual strings in one go, but it is some I see some developers doing. How does it fair in terms of bench marking.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"281\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083346-1024x281.png\" alt=\"\" class=\"wp-image-9\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083346-1024x281.png 1024w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083346-300x82.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083346-768x211.png 768w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083346.png 1104w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The simple string concatenations is almost twice as fast and 1\/3 the memory alloction.<\/p>\n\n\n\n<p>Lets ramp it up so the simple concatenation is actually doing 4 operations.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"695\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083757.png\" alt=\"\" class=\"wp-image-11\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083757.png 728w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083757-300x286.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"316\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083742-1024x316.png\" alt=\"\" class=\"wp-image-12\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083742-1024x316.png 1024w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083742-300x93.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083742-768x237.png 768w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-083742.png 1104w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here we can see StringBuilder beings to outperform simple concatenation in terms of pure speed, but slightly higher memory allocation.<\/p>\n\n\n\n<p>What happens if we make are strings much larger?<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084408-1024x585.png\" alt=\"\" class=\"wp-image-13\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084408-1024x585.png 1024w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084408-300x171.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084408-768x439.png 768w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084408.png 1131w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"231\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084353-1024x231.png\" alt=\"\" class=\"wp-image-14\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084353-1024x231.png 1024w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084353-300x68.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084353-768x173.png 768w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-084353.png 1032w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Interestingly StringBuilder is slower in this example and still has higher memory allocation.<\/p>\n\n\n\n<p>What happens if we double our 4 small string concatenation operations to 8?<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"737\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085625.png\" alt=\"\" class=\"wp-image-16\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085625.png 700w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085625-285x300.png 285w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"986\" height=\"229\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085649.png\" alt=\"\" class=\"wp-image-17\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085649.png 986w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085649-300x70.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085649-768x178.png 768w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/><\/figure>\n\n\n\n<p>Here we are begining to see what we would expected, StringBuilder is out performing simple concatenation in terms of speed and memory allocation.<\/p>\n\n\n\n<p>Lets check again with 12 string operations.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"650\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085924.png\" alt=\"\" class=\"wp-image-18\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085924.png 676w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-085924-300x288.png 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"221\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/image.png\" alt=\"\" class=\"wp-image-19\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/image.png 964w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/image-300x69.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/image-768x176.png 768w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><\/figure>\n\n\n\n<p>Again StringBuilder is outperforming simple concatenation.<\/p>\n\n\n\n<p>Lets take it up to 100 concatenations<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"493\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090415.png\" alt=\"\" class=\"wp-image-20\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090415.png 683w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090415-300x217.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"210\" src=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090552-1024x210.png\" alt=\"\" class=\"wp-image-21\" srcset=\"https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090552-1024x210.png 1024w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090552-300x62.png 300w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090552-768x158.png 768w, https:\/\/davidlindon.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-06-090552.png 1092w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here we really start to see the benefit of StringBuilder.<\/p>\n\n\n\n<p class=\"is-style-text-subtitle is-style-text-subtitle--1\">Summary<\/p>\n\n\n\n<p>In my testing, StringBuilder didn&#8217;t really pay off until concatenating> 6 strings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve seen developers use StringBuilder to concatenate 2 small strings in C# and I started thinking, are we so used to reaching for StringBuilder that we never stop and think, is this overkill? So I wanted to run some benchmarks and find the sweat spot in terms of speed and memory allocation where it makes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidlindon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6"}],"version-history":[{"count":3,"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/davidlindon.com\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/davidlindon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidlindon.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidlindon.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}