<div><b>[실험1]</b></div> <div><br></div> <div> static void Main(string[] args)</div> <div> {</div> <div> DateTime dt = DateTime.Now;</div> <div> for (int i = 0; i < 10000000; i++)</div> <div> {</div> ABC abc = default(ABC); <div> //ABC abc = new ABC();</div> <div><span style="font-size:9pt;line-height:1.5;"> //ABC abc = new ABC(null);</span></div> <div> }</div> <div> Console.WriteLine((DateTime.Now - dt).Milliseconds);</div> <div> }</div> <div><br></div> <div> public unsafe struct ABC</div> <div> {</div> <div> public fixed int array[200];</div> <div> public ABC(int[] _param) { }</div> <div> }</div> <div><br></div> <div><br></div> <div><br></div> <div>이런 코드가 있습니다.</div> <div>사용할 수 있는 ABC의 생성자는 3가지죠.</div> <div><br></div> <div><br></div> <div>default(ABC);</div> <div>new ABC();</div> <div>new ABC(null);</div> <div><br></div> <div><br></div> <div><br></div> <div>이렇게 3개를 테스해봤습니다.</div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div> <div>default(ABC); // 220ms</div> <div>new ABC(); // 220ms</div> <div>new ABC(null); // <font color="#ff0000">20ms</font></div></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div>default(ABC)와 new ABC()는 같은 로직이라고 생각이되네요.</div> <div>그런데 new ABC(null)은 10배나 빠른 속도를 나타내네요.</div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><b>[실험2]</b></div> <div><br></div> <div>그래서 혹시 null이라서 값을 못쓰는거 아닌가? 하고 좀더 추가해봤습니다.</div> <div>Set과 Add함수를 만들고 사용한것입니다.</div> <div><br></div> <div> <div><font color="#ff0000">빨간색은 위 소스에서 바뀌거나 추가된것입니다.</font></div> <div><br></div></div> <div> <div> <div> static void Main(string[] args)</div> <div> {</div> <div> DateTime dt = DateTime.Now;</div> <div> <font color="#ff0000">int sum = 0;</font></div> <div> for (int i = 0; i < 10000000; i++)</div> <div> {</div> ABC abc = default(ABC);<br> //ABC abc = new ABC(); <div> //ABC abc = new ABC(null);</div> <div><br></div> <div> <font color="#ff0000">abc.Set(0, 1);</font></div> <div><span style="font-size:9pt;line-height:1.5;"><font color="#ff0000"> abc.Add(0, ref sum);</font></span></div> <div> }</div> <div> <font color="#ff0000"> Console.WriteLine(sum);</font></div> <div> Console.WriteLine((DateTime.Now - dt).Milliseconds);</div> <div> }</div> <div><br></div> <div> public unsafe struct ABC</div> <div> {</div> <div> public fixed int array[200];</div> <div> public ABC(int[] _param) { }</div> <div><br></div> <div> <font color="#ff0000"> public void Add(int idx, ref int sum)</font></div> <div><font color="#ff0000"> {</font></div> <div><font color="#ff0000"> fixed (int* p = array)</font></div> <div><font color="#ff0000"> {</font></div> <div><font color="#ff0000"> sum += p[idx];</font></div> <div><font color="#ff0000"> }</font></div> <div><font color="#ff0000"> }</font></div> <div><font color="#ff0000"> public void Set(int idx, int val)</font></div> <div><font color="#ff0000"> {</font></div> <div><font color="#ff0000"> fixed (int* p = array)</font></div> <div><font color="#ff0000"> {</font></div> <div><font color="#ff0000"> p[idx] = val;</font></div> <div><font color="#ff0000"> }</font></div> <div><font color="#ff0000"> }</font></div> <div> }</div></div> <div><br></div> <div><br></div> <div> <div>default(ABC); // <font color="#ff0000">300ms</font></div> <div>new ABC(); // <font color="#ff0000">300ms</font></div> <div>new ABC(null); // <font color="#ff0000">90ms</font></div></div> <div><br></div> <div><br></div> <div>??? 또 new ABC가 빠르게 나왔습니다.</div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div>[실험3]</div> <div><br></div> <div>Set을 1번만 하고 Add를 여러번 해봤습니다.</div> <div><br></div></div> <div> <div> <div> static void Main(string[] args)</div> <div> {</div> <div> DateTime dt = DateTime.Now;</div> <div> int sum = 0;</div> <div> for (int i = 0; i < <font color="#ff0000">100000</font>; i++)</div> <div> {</div> ABC abc = default(ABC);<br> //ABC abc = new ABC(); <div> //ABC abc = new ABC(null);</div> <div><br></div> <div> abc.Set(0, 1);</div> <div> <font color="#ff0000">for(int j=0; j<100; j++)</font></div> <div><font color="#ff0000"> {</font></div> <div><font color="#ff0000"> abc.Add(0, ref sum);</font></div> <div><font color="#ff0000"> }</font></div> <div> }</div> <div> Console.WriteLine(sum);</div> <div> Console.WriteLine((DateTime.Now - dt).Milliseconds);</div> <div> }</div> <div><br></div> <div> public unsafe struct ABC</div> <div> {</div> <div> public fixed int array[200];</div> <div> public ABC(int[] _param) { }</div> <div><br></div> <div> public void Add(int idx, ref int sum)</div> <div> {</div> <div> fixed (int* p = array)</div> <div> {</div> <div> sum += p[idx];</div> <div> }</div> <div> }</div> <div> public void Set(int idx, int val)</div> <div> {</div> <div> fixed (int* p = array)</div> <div> {</div> <div> p[idx] = val;</div> <div> }</div> <div> }</div> <div> }</div></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div> <div>default(ABC); // <font color="#ff0000">50ms</font></div> <div>new ABC(); // <font color="#ff0000">50ms</font></div> <div>new ABC(null); // <font color="#ff0000">50ms</font></div></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div></div> <div>?????????</div> <div><br></div> <div>ABC의 속도가 느려질 수도있다고 생각했는데 하향평준화돼버렸습니다.</div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div><br></div> <div>이유가 뭘까요..? 궁금해죽겠네요.</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.