예제 #1
0
        private static void TypeStringDictionaryDispatch()
        {
            var types = ITest__MessageTable.GetMessageTypes();

            var msgLength = Math.Min(MessageCycle, types.GetLength(0));
            var table     = new Dictionary <string, Action <object> >();

            for (int i = 0; i < msgLength; i++)
            {
                table[types[i, 0].FullName] = (msg) => { };
            }

            RunTest("TypeStringDictionaryDispatch", msg =>
            {
                Action <object> handler;
                if (table.TryGetValue(msg.GetType().FullName, out handler))
                {
                    handler(msg);
                }
            });
        }
예제 #2
0
        private static void TypeDictionaryDispatch()
        {
            var types     = ITest__MessageTable.GetMessageTypes();
            var msgLength = Math.Min(MessageCycle, types.GetLength(0));

            // http://www.dotnetperls.com/dictionary-optimization
            var table = new Dictionary <Type, Action <object> >(msgLength * 5);

            for (int i = 0; i < msgLength; i++)
            {
                table[types[i, 0]] = (msg) => { };
            }

            RunTest("TypeDictionaryDispatch", msg =>
            {
                Action <object> handler;
                if (table.TryGetValue(msg.GetType(), out handler))
                {
                    handler(msg);
                }
            });
        }
예제 #3
0
        private static void RunTest(string testName, Action <object> test)
        {
            var types = ITest__MessageTable.GetMessageTypes();
            var msgs  = new object[Math.Min(MessageCycle, types.GetLength(0))];

            for (var i = 0; i < msgs.Length; i++)
            {
                msgs[i] = Activator.CreateInstance(types[i, 0]);
            }

            var sw = Stopwatch.StartNew();

            for (var i = 0; i < TestCount; i++)
            {
                test(msgs[i % msgs.Length]);
            }
            sw.Stop();

            var elapsed = sw.ElapsedMilliseconds;
            var unit    = (double)elapsed * 1000000 / TestCount;

            Console.WriteLine($"{testName,-30} {elapsed,6} ms {unit,2} ps");
        }