Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for String vs StringBuilder .NET (C#)
Bervianto Leo Pratama
Bervianto Leo Pratama

Posted on

     

String vs StringBuilder .NET (C#)

Hi, everyone!

Do you have any use cases to manipulate large strings? Have you ever wondered about optimizing your code? Let's explore the difference between the native string and the string builder. When should we use string builder? When could we use string manipulation directly? My premise is we should use the string builder when we have large string manipulation, and we can use string manipulation directly if we combine two or three strings.

Speed/Performance and Memory

I ran the Benchmark using .NET 8 and BenchmarkDotNet. The result may vary depending on your machine. My machine specs:

  1. Intel Core i7-9750H CPU @ 2.60GHz, 6 Core(s), 12 Logical Processor(s)
  2. RAM 32 GB, 2667 MHz

The Result

This is the raw result after running my Benchmark. I will explain case by case.

MethodNMeanErrorStdDevMedianRankGen0Gen1Gen2Allocated
AppendWithStringBuilder100010.45 us0.207 us0.548 us10.20 us18.75850.9613-53.91 KB
AppendWithStringBuilder10000213.85 us4.248 us7.768 us212.04 us266.650466.650466.6504435.63 KB
AppendWithString1000759.31 us15.032 us20.068 us756.16 us31755.859493.7500-10777.3 KB
PrependWithString1000761.74 us15.045 us25.137 us761.18 us31755.859493.7500-10777.3 KB
AppendWithStringBuilder1000001,491.00 us8.566 us7.153 us1,493.93 us4707.0313652.3438353.51564316.28 KB
PrependWithStringBuilder10003,652.40 us57.881 us51.310 us3,639.86 us519.53133.9063-123.07 KB
PrependWithString10000103,185.42 us1,756.825 us1,643.335 us102,605.36 us6316200.0000294200.0000290200.00001074665.57 KB
AppendWithString10000103,766.99 us2,047.758 us2,102.896 us103,956.12 us6316200.0000294200.0000290200.00001074665.57 KB
PrependWithStringBuilder10000443,622.50 us8,848.754 us9,087.019 us443,266.50 us7---1230.88 KB
AppendWithString10000031,694,035.38 us479,131.423 us424,737.527 us31,537,399.50 us818852000.000018809000.000018805000.0000107431164.26 KB
PrependWithString10000032,527,484.53 us133,803.969 us111,732.374 us32,527,375.30 us919962000.000019918000.000019914000.0000107431463.44 KB
PrependWithStringBuilder10000058,193,475.60 us712,873.020 us792,356.472 us58,134,526.30 us101000.0000--12305.1 KB

Append Case

MethodNMeanErrorStdDevMedianRankGen0Gen1Gen2Allocated
AppendWithStringBuilder100010.45 us0.207 us0.548 us10.20 us18.75850.9613-53.91 KB
AppendWithString1000759.31 us15.032 us20.068 us756.16 us31755.859493.7500-10777.3 KB
AppendWithStringBuilder10000213.85 us4.248 us7.768 us212.04 us266.650466.650466.6504435.63 KB
AppendWithString10000103,766.99 us2,047.758 us2,102.896 us103,956.12 us6316200.0000294200.0000290200.00001074665.57 KB
AppendWithStringBuilder1000001,491.00 us8.566 us7.153 us1,493.93 us4707.0313652.3438353.51564316.28 KB
AppendWithString10000031,694,035.38 us479,131.423 us424,737.527 us31,537,399.50 us818852000.000018809000.000018805000.0000107431164.26 KB

As you can see, the String Builder always wins. It's not only in terms of performance but also allocated memory. The ratio of performance and memory is high. You know, it's a big deal!

If you wonder why the string concat uses large memory when looping, it will copy the string N times. The string is immutable, so they need to copy the content.

Append String Builder

Append SB

Append String

Append String

Prepend Case

MethodNMeanErrorStdDevMedianRankGen0Gen1Gen2Allocated
PrependWithString1000761.74 us15.045 us25.137 us761.18 us31755.859493.7500-10777.3 KB
PrependWithStringBuilder10003,652.40 us57.881 us51.310 us3,639.86 us519.53133.9063-123.07 KB
PrependWithString10000103,185.42 us1,756.825 us1,643.335 us102,605.36 us6316200.0000294200.0000290200.00001074665.57 KB
PrependWithStringBuilder10000443,622.50 us8,848.754 us9,087.019 us443,266.50 us7---1230.88 KB
PrependWithString10000032,527,484.53 us133,803.969 us111,732.374 us32,527,375.30 us919962000.000019918000.000019914000.0000107431463.44 KB
PrependWithStringBuilder10000058,193,475.60 us712,873.020 us792,356.472 us58,134,526.30 us101000.0000--12305.1 KB

As you can see, the String Builder starts struggling after dealing with prepend. I will choose String Builder if I have memory concerns. Of course, there is a tread-off. I rarely do prepend the string. If needed to do so, I might choose a different approach.

If you need to learn more about the difference between String and String Builder, please visit thisdocumentation.

Prepend String Builder

Prepend String Builder

Prepend String

Prepend String

Benchmark Code

GitHub logo bervProject / StringBuilderPerformance

String Builder Performance

StringBuilderPerformance

String Builder Performance

Summary

MethodNMeanErrorStdDevMedianRankGen0Gen1Gen2Allocated
AppendWithStringBuilder100010.45 us0.207 us0.548 us10.20 us18.75850.9613-53.91 KB
AppendWithStringBuilder10000213.85 us4.248 us7.768 us212.04 us266.650466.650466.6504435.63 KB
AppendWithString1000759.31 us15.032 us20.068 us756.16 us31755.859493.7500-10777.3 KB
PrependWithString1000761.74 us15.045 us25.137 us761.18 us31755.859493.7500-10777.3 KB
AppendWithStringBuilder1000001,491.00 us8.566 us7.153 us1,493.93 us4707.0313652.3438353.51564316.28 KB
PrependWithStringBuilder10003,652.40 us57.881 us51.310 us3,639.86 us519.53133.9063-123.07 KB
PrependWithString10000103,185.42 us1,756.825 us1,643.335 us102,605.36 us6316200.0000294200.0000290200.00001074665.57 KB
AppendWithString10000103,766.99 us2,047.758 us2,102.896 us103,956.12 us6316200.0000294200.0000290200.00001074665.57 KB
PrependWithStringBuilder10000443,622.50 us8,848.754 us

Thank you

Do you have any feedback? Feel free to drop your comment. Thank you for reading. :)

GIF Thanks

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

AWS Community Builder | Software Engineer | Focus on topics: Microservices, Cloud Computing, and Cyber Security.
  • Location
    Bandung, Indonesia
  • Education
    Bachelor of Science, Computer Science
  • Pronouns
    He/His/Him
  • Work
    Software Engineer
  • Joined

More fromBervianto Leo Pratama

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp