Translate

2017年5月13日土曜日

C# 型生成コスト比較

諸事情により比較と結果をのっけておく。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace ConsoleApplication9
{
    class Program
    {
        private static readonly int Counter = 1000000;

        static void Main(string[] args)
        {
            Console.WriteLine("■WriteOnly");

            TimeSpan t1 = Program.HashTableData(false);
            Log log = new Log(t1);
            log.Output("HashTable");

            TimeSpan t2 = Program.DictionaryData(false);
            log.Output("Dictionary", t2);

            TimeSpan t3 = Program.StructData(false);
            log.Output("Struct", t3);

            TimeSpan t4 = Program.ClassData(false);
            log.Output("Class", t4);

            Console.WriteLine("■Write/Read");

            TimeSpan t5 = Program.HashTableData(true);
            log = new Log(t5);
            log.Output("HashTable");

            TimeSpan t6 = Program.DictionaryData(true);
            log.Output("Dictionary", t6);

            TimeSpan t7 = Program.StructData(true);
            log.Output("Struct", t7);

            TimeSpan t8 = Program.ClassData(true);
            log.Output("Class", t8);
        }

        static TimeSpan HashTableData(bool flag)
        {
            Stopwatch sw = Stopwatch.StartNew();

            List<hashtable> data = new List<hashtable>();
            foreach (int num in Enumerable.Range(1, Counter))
            {
                Hashtable ht = new Hashtable();
                ht["a"] = "a";
                ht["b"] = "b";
                ht["c"] = "c";
                ht["d"] = "d";
                ht["e"] = "e";
                data.Add(ht);
            }

            if (flag)
            {
                foreach (var d in data)
                {
                    string val1 = (string)d["a"];
                    string val2 = (string)d["b"];
                    string val3 = (string)d["c"];
                    string val4 = (string)d["d"];
                    string val5 = (string)d["e"];
                }
            }

            return sw.Elapsed;
        }

        static TimeSpan DictionaryData(bool flag)
        {
            Stopwatch sw = Stopwatch.StartNew();

            List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
            foreach (int num in Enumerable.Range(1, Counter))
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("a", "a");
                dic.Add("b", "b");
                dic.Add("c", "c");
                dic.Add("d", "d");
                dic.Add("e", "e");
                data.Add(dic);
            }

            if (flag)
            {
                foreach (var d in data)
                {
                    string val1 = d["a"];
                    string val2 = d["b"];
                    string val3 = d["c"];
                    string val4 = d["d"];
                    string val5 = d["e"];
                }
            }

            return sw.Elapsed;
        }

        static TimeSpan StructData(bool flag)
        {
            Stopwatch sw = Stopwatch.StartNew();

            List<structdatatype> data = new List<structdatatype>();
            foreach (int num in Enumerable.Range(1, Counter))
            {
                structDataType d = new structDataType();
                d.a = "a";
                d.b = "b";
                d.c = "c";
                d.d = "d";
                d.e = "e";
                data.Add(d);
            }

            if (flag)
            {
                foreach (var d in data)
                {
                    string val1 = d.a;
                    string val2 = d.b;
                    string val3 = d.c;
                    string val4 = d.d;
                    string val5 = d.e;
                }
            }

            return sw.Elapsed;
        }

        static TimeSpan ClassData(bool flag)
        {
            Stopwatch sw = Stopwatch.StartNew();

            List<hoge> data = new List<hoge>();
            foreach (int num in Enumerable.Range(1, Counter))
            {
                hoge g = new hoge();
                g.a = "a";
                g.b = "b";
                g.c = "c";
                g.d = "d";
                g.e = "e";
                data.Add(g);
            }

            if (flag)
            {
                foreach (var d in data)
                {
                    string val1 = d.a;
                    string val2 = d.b;
                    string val3 = d.c;
                    string val4 = d.d;
                    string val5 = d.e;
                }
            }

            return sw.Elapsed;
        }

        private struct structDataType
        {
            public string a;
            public string b;
            public string c;
            public string d;
            public string e;
        }
    }

    class hoge
    {
        public string a;
        public string b;
        public string c;
        public string d;
        public string e;
    }

    class Log
    {
        public Log(TimeSpan baseTime)
        {
            this.BaseTime = baseTime;
        }
        
        private TimeSpan BaseTime { get; set; }


        public void Output(string Name)
        {
            string ret = string.Format("{0, -10}{1}", Name, this.BaseTime);
            Console.WriteLine(ret);
        }

        public void Output(string Name, TimeSpan time)
        {
            string ret = string.Format("{0, -10}{1}({2:F1}%)", Name, time, time.TotalMilliseconds / this.BaseTime.TotalMilliseconds * 100);
            Console.WriteLine(ret);
        }
    }
}

■WriteOnly
HashTable 00:00:01.1076920
Dictionary00:00:01.2820160(115.7%)
Struct    00:00:00.0399449(3.6%)
Class     00:00:00.2202693(19.9%)
■Write/Read
HashTable 00:00:01.4136740
Dictionary00:00:01.5449535(109.3%)
Struct    00:00:00.0694128(4.9%)
Class     00:00:00.2186330(15.5%)

0 件のコメント: