본문 바로가기
개발/C#

c# List나 Dictionary 의 capacity 설정

by MNMNMNMN 2021. 2. 23.
728x90

c# List나 Dictionary 의 capacity 설정

capacity크기를 조정하지 않고 포함될 수 있는 요소 수입니다.

혹시 ListDictionary를 사용할때 미리 메모리를 할당하면 좋을까?

한번 찾아봤다.


C# program that tests capacity

using System;
using System.Collections.Generic;

class Program
{
    const int _m = 100000;
    static List<string> _values = new List<string>();

    public static void Main()
    {
        // Add 100 strings for testing.
        for (int i = 0; i < 100; i++)
        {
            _values.Add("value" + i.ToString());
        }

        long t1 = Environment.TickCount;

        A_Dictionary();

        long t2 = Environment.TickCount;

        B_DictionaryCapacity();

        long t3 = Environment.TickCount;

        C_DictionaryCapacity();

        long t4 = Environment.TickCount;

        D_DictionaryCapacity();

        long t5 = Environment.TickCount;

        E_List();

        long t6 = Environment.TickCount;

        F_ListCapacity();

        long t7 = Environment.TickCount;

        // Write Dictionary times.
        Console.WriteLine("A_Dictionary: " + (t2 - t1) + " ms");
        Console.WriteLine("B_DictionaryCapacity: " + (t3 - t2) + " ms");
        Console.WriteLine("C_DictionaryCapacity: " + (t4 - t3) + " ms");
        Console.WriteLine("D_DictionaryCapacity: " + (t5 - t4) + " ms");

        // Write List times.
        Console.WriteLine("E_List: " + (t6 - t5) + " ms");
        Console.WriteLine("F_ListCapacity: " + (t7 - t6) + " ms");
    }

    static void A_Dictionary()
    {
        // No capacity.
        for (int i = 0; i < _m; i++)
        {
            var d = new Dictionary<string, int>();
            foreach (string k in _values)
            {
                d.Add(k, 0);
            }
        }
    }

    static void B_DictionaryCapacity()
    {
        // Capacity from collection Count.
        for (int i = 0; i < _m; i++)
        {
            var d = new Dictionary<string, int>(_values.Count);
            foreach (string k in _values)
            {
                d.Add(k, 0);
            }
        }
    }

    static void C_DictionaryCapacity()
    {
        // Const capacity.
        for (int i = 0; i < _m; i++)
        {
            var d = new Dictionary<string, int>(100);
            foreach (string k in _values)
            {
                d.Add(k, 0);
            }
        }
    }

    static void D_DictionaryCapacity()
    {
        // Huge capacity (10 times too large).
        for (int i = 0; i < _m; i++)
        {
            var d = new Dictionary<string, int>(1000);
            foreach (string k in _values)
            {
                d.Add(k, 0);
            }
        }
    }

    static void E_List()
    {
        // No capacity.
        for (int i = 0; i < _m * 5; i++)
        {
            var l = new List<string>();
            foreach (string k in _values)
            {
                l.Add(k);
            }
        }
    }

    static void F_ListCapacity()
    {
        // Exact capacity.
        for (int i = 0; i < _m * 5; i++)
        {
            var l = new List<string>(100);
            foreach (string k in _values)
            {
                l.Add(k);
            }
        }
    }
}

A_Dictionary: 500 ms
B_DictionaryCapacity: 328 ms
C_DictionaryCapacity: 329 ms
D_DictionaryCapacity: 484 ms
E_List: 547 ms
F_ListCapacity: 437 ms

 

[출처]www.dotnetperls.com/capacity

 


결론은 미리 사이즈를 알수 있으면 ListDictionary든 생성할때 부터 사이즈를 설정하는게 좋은것 같다.

 

[참고]boycoding.tistory.com/223

728x90
반응형

댓글