All pastes #728360 Raw Edit

tobz

public text v1 · immutable
·published 2007-10-06 22:18 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TypeLookupSpeed
{
    unsafe class Program
    {
        static Dictionary<Type, Object> m_typeObjLookup;
        static Dictionary<int, Object> m_intObjLookup;
        static Dictionary<RuntimeTypeHandle, Object> m_rthObjLookup;
        static Dictionary<IntPtr, Object> m_intptrObjLookup;
        static Dictionary<long, Object> m_longObjLookup;

        static unsafe void Main(string[] args)
        {
            m_typeObjLookup = new Dictionary<Type, Object>();
            m_intObjLookup = new Dictionary<int, Object>();
            m_intptrObjLookup = new Dictionary<IntPtr, Object>();
            m_rthObjLookup = new Dictionary<RuntimeTypeHandle, Object>();
            m_longObjLookup = new Dictionary<long, Object>();

            m_typeObjLookup.Add(typeof(sbyte), new object());
            m_typeObjLookup.Add(typeof(byte), new object());
            m_typeObjLookup.Add(typeof(short), new object());
            m_typeObjLookup.Add(typeof(ushort), new object());
            m_typeObjLookup.Add(typeof(int), new object());
            m_typeObjLookup.Add(typeof(uint), new object());
            m_typeObjLookup.Add(typeof(long), new object());
            m_typeObjLookup.Add(typeof(ulong), new object());
            m_typeObjLookup.Add(typeof(char), new object());
            m_typeObjLookup.Add(typeof(float), new object());
            m_typeObjLookup.Add(typeof(decimal), new object());
            m_typeObjLookup.Add(typeof(double), new object());
            m_typeObjLookup.Add(typeof(bool), new object());
            m_typeObjLookup.Add(typeof(string), new object());

            m_intObjLookup.Add(typeof(sbyte).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(byte).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(short).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(ushort).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(int).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(uint).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(long).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(ulong).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(char).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(float).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(decimal).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(double).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(bool).TypeHandle.Value.ToInt32(), new object());
            m_intObjLookup.Add(typeof(string).TypeHandle.Value.ToInt32(), new object());

            m_intptrObjLookup.Add(typeof(sbyte).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(byte).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(short).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(ushort).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(int).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(uint).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(long).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(ulong).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(char).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(float).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(decimal).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(double).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(bool).TypeHandle.Value, new object());
            m_intptrObjLookup.Add(typeof(string).TypeHandle.Value, new object());

            m_rthObjLookup.Add(typeof(sbyte).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(byte).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(short).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(ushort).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(int).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(uint).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(long).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(ulong).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(char).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(float).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(decimal).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(double).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(bool).TypeHandle, new object());
            m_rthObjLookup.Add(typeof(string).TypeHandle, new object());

            m_longObjLookup.Add(typeof(sbyte).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(byte).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(short).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(ushort).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(int).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(uint).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(long).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(ulong).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(char).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(float).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(decimal).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(double).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(bool).TypeHandle.Value.ToInt64(), new object());
            m_longObjLookup.Add(typeof(string).TypeHandle.Value.ToInt64(), new object());

            Stopwatch timer = new Stopwatch();

            object obj;

            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                obj = m_typeObjLookup[typeof(sbyte)];
                obj = m_typeObjLookup[typeof(byte)];
                obj = m_typeObjLookup[typeof(short)];
                obj = m_typeObjLookup[typeof(ushort)];
                obj = m_typeObjLookup[typeof(int)];
                obj = m_typeObjLookup[typeof(uint)];
                obj = m_typeObjLookup[typeof(long)];
                obj = m_typeObjLookup[typeof(ulong)];
                obj = m_typeObjLookup[typeof(char)];
                obj = m_typeObjLookup[typeof(float)];
                obj = m_typeObjLookup[typeof(decimal)];
                obj = m_typeObjLookup[typeof(double)];
                obj = m_typeObjLookup[typeof(bool)];
                obj = m_typeObjLookup[typeof(string)];
            }
            timer.Stop();

            Console.WriteLine("100,000 iterations of retrieve-by-type: {0}ms", timer.ElapsedMilliseconds);

            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            timer.Reset();
            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                obj = m_intObjLookup[typeof(sbyte).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(byte).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(short).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(ushort).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(int).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(uint).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(long).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(ulong).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(char).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(float).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(decimal).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(double).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(bool).TypeHandle.Value.ToInt32()];
                obj = m_intObjLookup[typeof(string).TypeHandle.Value.ToInt32()];
            }
            timer.Stop();

            Console.WriteLine("100,000 iterations of retrieve-by-runtime-type-pointer-value: {0}ms", timer.ElapsedMilliseconds);

            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            timer.Reset();
            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                obj = m_intptrObjLookup[typeof(sbyte).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(byte).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(short).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(ushort).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(int).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(uint).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(long).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(ulong).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(char).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(float).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(decimal).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(double).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(bool).TypeHandle.Value];
                obj = m_intptrObjLookup[typeof(string).TypeHandle.Value];
            }
            timer.Stop();

            Console.WriteLine("100,000 iterations of retrieve-by-runtime-type-pointer: {0}ms", timer.ElapsedMilliseconds);

            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            timer.Reset();
            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                obj = m_rthObjLookup[typeof(sbyte).TypeHandle];
                obj = m_rthObjLookup[typeof(byte).TypeHandle];
                obj = m_rthObjLookup[typeof(short).TypeHandle];
                obj = m_rthObjLookup[typeof(ushort).TypeHandle];
                obj = m_rthObjLookup[typeof(int).TypeHandle];
                obj = m_rthObjLookup[typeof(uint).TypeHandle];
                obj = m_rthObjLookup[typeof(long).TypeHandle];
                obj = m_rthObjLookup[typeof(ulong).TypeHandle];
                obj = m_rthObjLookup[typeof(char).TypeHandle];
                obj = m_rthObjLookup[typeof(float).TypeHandle];
                obj = m_rthObjLookup[typeof(decimal).TypeHandle];
                obj = m_rthObjLookup[typeof(double).TypeHandle];
                obj = m_rthObjLookup[typeof(bool).TypeHandle];
                obj = m_rthObjLookup[typeof(string).TypeHandle];
            }
            timer.Stop();

            Console.WriteLine("100,000 iterations of retrieve-by-runtime-type-handle: {0}ms", timer.ElapsedMilliseconds);

            GC.Collect(2, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();

            timer.Reset();
            timer.Start();
            for (int i = 0; i < 100000; i++)
            {
                obj = m_longObjLookup[typeof(sbyte).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(byte).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(short).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(ushort).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(int).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(uint).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(long).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(ulong).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(char).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(float).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(decimal).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(double).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(bool).TypeHandle.Value.ToInt64()];
                obj = m_longObjLookup[typeof(string).TypeHandle.Value.ToInt64()];
            }
            timer.Stop();

            Console.WriteLine("100,000 iterations of retrieve-by-runtime-type-handle-actual-pointer: {0}ms", timer.ElapsedMilliseconds);

            Console.ReadLine();
        }
    }
}