Пример #1
0
        private static void RunArrayBenchmark()
        {
            ArrayElementGetter getter = null;
            ArrayElementSetter setter = null;

            var initMap = new Dictionary <string, Action>
            {
                { "Init setter", () => { setter = PeopleArray.GetType().DelegateForSetElement(); } },
                { "Init getter", () => { getter = PeopleArray.GetType().DelegateForGetElement(); } }
            };

            var actionMap = new Dictionary <string, Action>
            {
                { "Direct set", () => { PeopleArray[5] = null; } },
                { "Direct get", () => { var person = PeopleArray[5]; } },
                { "Reflection set", () => ((Array)(object)PeopleArray).SetValue(null, 5) },
                { "Reflection get", () => ((Array)(object)PeopleArray).GetValue(5) },
                { "Fasterflect set", () => PeopleArray.SetElement(5, null) },
                { "Fasterflect get", () => PeopleArray.GetElement(5) },
                { "Fasterflect cached set", () => setter(PeopleArray, 5, null) },
                { "Fasterflect cached get", () => getter(PeopleArray, 5) },
            };

            Execute("Benchmark for Array Element Access", initMap, actionMap);
        }
Пример #2
0
        /// <summary>
        /// Creates an <see cref="ArrayElementGetter"/> which retrieves an element of an array.
        /// </summary>
        /// <param name="arrayType">The <see cref="Type"/> of the array's elements.</param>
        /// <returns>An <see cref="ArrayElementGetter"/> which retrieves an element of an array.</returns>
        public static ArrayElementGetter ArrayGetter(Type arrayType)
        {
            ArrayElementGetter value = ArrayGetters.Get(arrayType);

            if (value != null)
            {
                return(value);
            }
            value = (ArrayElementGetter) new ArrayGetEmitter(arrayType).GetDelegate();
            ArrayGetters.Insert(arrayType, value);
            return(value);
        }