๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
C#

StringBuilder

by LemongO 2024. 5. 24.

๐Ÿ“ŒStringBuilder๋ž€?

String์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜์ • ์‹œ,
์ƒˆ๋กœ์šด String ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— GC ๋Œ€์ƒ์ด๋ฉฐ ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ๋–จ์–ด์ง„๋‹ค.
๋Œ€์•ˆ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๊ฐ€ StringBuilder ํด๋ž˜์Šค์ด๋‹ค.
StringBuilder๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ธฐ์กด ๊ฐ์ฒด์—์„œ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์ด ํฌ๊ฒŒ ํ–ฅ์ƒ๋œ๋‹ค.

 

๐Ÿ“ŒStringBuilder ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

System.Text ๋„ค์ž„์ŠคํŽ˜์ด์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

StringBuilder ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ ํ›„

Append, Insert, Replace, Remove ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

using System.Text;
StringBuilder sb = new StringBuilder();
sb.Append("append");
sb.Append("string");

 

StringBuilder๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ,
๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌธ์ž ์ˆ˜๊ฐ€ ์ง€์ •๋˜๊ณ  ๊ทธ ์ˆ˜๊ฐ€ ๋„˜์–ด๊ฐ€๊ธฐ ์ „ ๊นŒ์ง„ ํ•ด๋‹น ๊ณต๊ฐ„ ๋‚ด์—์„œ ์ˆ˜์ •์ด ์ด๋ฃจ์–ด์ง„๋‹ค.

StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.Capacity);

 

์•„๋ฌด๋Ÿฐ ์„ค์ •์„ ํ•˜์ง€ ์•Š์œผ๋ฉด 16๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋ณด๊ด€ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค.

์ฆ‰ ์„ค์ •์„ ํ•ด์„œ ์ดˆ๊ธฐ ์šฉ๋Ÿ‰์„ ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

StringBuilder sb = new StringBuilder();
sb.Capacity = 25;
Console.WriteLine(sb.Capacity);

 

Capacity ํ”„๋กœํผํ‹ฐ์™€ Length ํ”„๋กœํผํ‹ฐ๋กœ ํ˜„์žฌ StringBuilder์˜ ์šฉ๋Ÿ‰๊ณผ ํ• ๋‹น๋œ ๋ฌธ์ž์˜ ๊ธธ์ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

StringBuilder sb = new StringBuilder();
sb.Capacity = 25;
for (int i = 0; i < 20; i++)
    sb.Append("a");

Console.WriteLine($"์šฉ๋Ÿ‰ : {sb.Capacity}");
Console.WriteLine($"๊ธธ์ด : {sb.Length}");
Console.WriteLine(sb.ToString());

 

์ €์žฅ๋œ ๊ณต๊ฐ„์ด ์šฉ๋Ÿ‰์„ ์ดˆ๊ณผํ•˜๊ฒŒ๋˜๋ฉด ์ž๋™์œผ๋กœ ๊ณต๊ฐ„์ด ํ• ๋‹น๋˜๊ณ  ์šฉ๋Ÿ‰์ด 2๋ฐฐ๋กœ ์ฆ๊ฐ€ํ•œ๋‹ค.

StringBuilder sb = new StringBuilder();
sb.Capacity = 25;
for (int i = 0; i < 30; i++)
    sb.Append("a");

Console.WriteLine($"์šฉ๋Ÿ‰ : {sb.Capacity}");
Console.WriteLine($"๊ธธ์ด : {sb.Length}");
Console.WriteLine(sb.ToString());

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ์•Œ์•„๋ดค๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋งˆ์ดํฌ๋กœ์†Œํ”„ํŠธ ํŽ˜์ด์ง€์— ๋‚˜์™€์žˆ๋‹ค.

https://learn.microsoft.com/ko-kr/dotnet/standard/base-types/stringbuilder

 

.NET์—์„œ StringBuilder ํด๋ž˜์Šค ์‚ฌ์šฉ - .NET

.NET์—์„œ StringBuilder ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒˆ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋ฌธ์ž์—ด์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

learn.microsoft.com

 

 

๐Ÿ“Œ์†๋„์ฐจ์ด

๊ทธ๋ ‡๋‹ค๋ฉด ์†๋„๋Š” ์–ผ๋งˆ๋‚˜ ์ฐจ์ด๊ฐ€๋‚ ๊นŒ?

์‹คํ—˜์„ ์œ„ํ•ด string += "a"์„ 1,000,000๋ฒˆ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค๋ดค๋‹ค.

DateTime startTime = DateTime.Now;
Console.WriteLine("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘ ์‹œ๊ฐ„: " + startTime.ToString());

string st = "a";
for (int i = 0; i < 1000000; i++)
    st += "a";

DateTime endTime = DateTime.Now;
Console.WriteLine("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ ์‹œ๊ฐ„: " + endTime.ToString());

TimeSpan duration = endTime - startTime;
Console.WriteLine("์ด ์‹คํ–‰ ์‹œ๊ฐ„: " + duration.ToString());

์ฒ˜์Œ์—” ์ฝ˜์†”์ด ๋ฉˆ์ถ˜์ค„ ์•Œ์•˜๋‹ค. ์•ฝ 2๋ถ„ 28์ดˆ์˜ ์ƒ๋‹นํ•œ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด StringBuilder๋Š” ์–ด๋–จ๊นŒ?

 

DateTime startTime = DateTime.Now;
Console.WriteLine("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘ ์‹œ๊ฐ„: " + startTime.ToString());

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000000; i++)
    sb.Append("a");
    
DateTime endTime = DateTime.Now;
Console.WriteLine("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ ์‹œ๊ฐ„: " + endTime.ToString());

TimeSpan duration = endTime - startTime;
Console.WriteLine("์ด ์‹คํ–‰ ์‹œ๊ฐ„: " + duration.ToString());

 

......

๊ทธ๋ƒฅ ๋ง๋„ ์•ˆ ๋˜๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค.

 

๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜์ •์„ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—”

StringBuilder๋Š” ์„ ํƒ์ด ์•„๋‹ˆ๋ผ ํ•„์ˆ˜์ธ๋“ฏ ํ•˜๋‹ค.