문자열은 참조유형이다 이를 증명하기 위해 테스트를 해보자

 

타임아웃을 사용하여 문자열과 숫자타입 같은문자열, 문자열 연결, StringBuilder 의 예제이다

 

아래 결과를 확인해보면 완료되는 시간을 확인 할 수 있는데 

참조유형과 값유형의 시간이 많이 차이가 난다.

 

그리고 같은 문자열을 입력한경우 같은 메모리를 공유하는것을 알 수 있다.

다른문자열을 입력 할 경우  기존에 메모리에 올려놨던 내용은  garbage collection으로 이동하게된다.

 

stringbuilder를 사용하면 문자열을 조합할때마다 새로운 변수를 생성하지 않고 결합할 수 있다.

내부에 함수가 존재해서 값들을 조합하거나 삭제할때에도 새로운 인스턴스가 생성되지 않는다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Diagnostics;
using System.Text;
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //string type
            StringsImmutable();
            Console.WriteLine();
            Console.WriteLine();
            //integer Type
            IntegerType();
            Console.WriteLine();
            Console.WriteLine();
            SameString();
            Console.WriteLine();
            Console.WriteLine();
            StringConcat();
            Console.WriteLine();
            Console.WriteLine();
            StringBuilder();
 
            Console.ReadKey();
        }
 
 
        public static void StringsImmutable()
        {
            Console.WriteLine("StringsImmutable");
            string str = "";
            Console.WriteLine("Loop Start");
 
            var stopwatch = new Stopwatch();
 
            stopwatch.Start();
 
            for (int i = 0; i < 3000000; i++)
            {
                //유니크한 아이디 생성
                str = Guid.NewGuid().ToString();
 
            }
            stopwatch.Stop();
 
            Console.WriteLine("Loop End");
            Console.WriteLine($"Loop Excution Time In MS {stopwatch.ElapsedMilliseconds}");
          
        }
 
        public static void IntegerType()
        {
            Console.WriteLine("IntegerType");
            int ctr = 0;
            Console.WriteLine("Loop Start");
 
            var stopwatch = new Stopwatch();
 
            stopwatch.Start();
 
            for (int i = 0; i < 3000000; i++)
            {
                ctr = ctr + 1;
 
 
            }
            stopwatch.Stop();
 
            Console.WriteLine("Loop End");
            Console.WriteLine($"Loop Excution Time In MS {stopwatch.ElapsedMilliseconds}");
 
        }
 
        public static void SameString()
        {
            Console.WriteLine("SameString");
            string str = "";
            Console.WriteLine("Loop Start");
 
            var stopwatch = new Stopwatch();
 
            stopwatch.Start();
 
            for (int i = 0; i < 3000000; i++)
            {
                str = "같은문자";
 
 
            }
            stopwatch.Stop();
            //메모리에 이미 있는지 확인 후 있다면 같다면 동일한 메모리 
            Console.WriteLine("Loop End");
            Console.WriteLine($"Loop Excution Time In MS {stopwatch.ElapsedMilliseconds}");
        }
 
        public static void StringConcat()
        {
            Console.WriteLine("StringConcat");
            string ctr = "";
            Console.WriteLine("Loop Start");
 
            var stopwatch = new Stopwatch();
 
            stopwatch.Start();
 
            for (int i = 0; i < 30000; i++)
            {
                ctr = "같은문자" + ctr ;
 
 
            }
            stopwatch.Stop();
            // 메모리생성 > 새로운 메모리 생성 > 첫번째 메모리는 garbage collection으로 이동
            Console.WriteLine("Loop End");
            Console.WriteLine($"Loop Excution Time In MS {stopwatch.ElapsedMilliseconds}");
        }
 
        public static void StringBuilder()
        {
            StringBuilder stringBuilder = new StringBuilder();
            Console.WriteLine("stringBuilder");
            Console.WriteLine("Loop Start");
 
            var stopwatch = new Stopwatch();
 
            stopwatch.Start();
 
            for (int i = 0; i < 30000; i++)
            {
                stringBuilder.Append("stringBuilder");
 
            }
 
            stopwatch.Stop();
            // 메모리생성 > 새로운 메모리 생성 > 첫번째 메모리는 garbage collection으로 이동
            Console.WriteLine("Loop End");
            Console.WriteLine($"Loop Excution Time In MS {stopwatch.ElapsedMilliseconds}");
 
        }
 
 
    }
}
 
cs
 

 

'ASP.NET' 카테고리의 다른 글

3. .NET Framework CLR  (0) 2020.04.20
2. .NET Framework 란?  (0) 2020.04.20
7. 닷넷 기초 (이미지 컨트롤)  (0) 2018.02.25
6. 닷넷 기초 (현재시간 출력하기)  (0) 2018.02.25
5. 닷넷 기초 (표준 컨트롤러)  (0) 2018.02.25

+ Recent posts