Esempio n. 1
0
        static int MeasureTupleVsKeyValuePairInDictionaryLookup()
        {
            const int iterations = 1000000;

            int ran = 0;

            var tupleMap  = new DictionaryEx <Tuple <int, int, int, DateTime>, int>(200);
            var structMap = new DictionaryEx <StructTuple <int, int, int, DateTime>, int>(200);

            for (int i = 200 - 1; i >= 0; i--)
            {
                tupleMap.Add(new Tuple <int, int, int, DateTime>(400 - i, i + 1, i - 1, new DateTime(1990 + i % 10, 5, 2)), 0);
                structMap.Add(new StructTuple <int, int, int, DateTime>(400 - i, i + 1, i - 1, new DateTime(1990 + i % 10, 5, 2)), 0);
            }

            //CodeTimer.Time(true, "Tuple lookup",
            //    iterations,
            //    () =>
            //    {
            //        int value;
            //        if (tupleMap.TryGetValue(new Tuple<int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2)), out value))
            //            ran += 1;
            //    });

            CodeTimer.Time(true, "Struct lookup outer add or update",
                           iterations,
                           () =>
            {
                var key = new StructTuple <int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2));
                int a   = 1;
                int value;
                if (structMap.TryGetValue(key, out value))
                {
                    structMap[key] = value + a;
                }
                else
                {
                    structMap.Add(key, a);
                }
                ran += 1;
            });

            CodeTimer.Time(true, "Struct lookup AddOrUpdate",
                           iterations,
                           () =>
            {
                int a = 1;
                structMap.AddOrUpdate(
                    new StructTuple <int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2)),
                    a,
                    (k, cv, nv) => cv + nv);
                ran += 1;
            });

            return(ran);
        }
Esempio n. 2
0
        static long MeasureStringBuilderVsStringFormat()
        {
            const int iterations = 1000000;

            long ran = 0;

            int a = 1, b = -1;

            var x = new StructTuple <int, int, int>(100, 102, 300);

            CodeTimer.Time(
                true,
                "string.Format()",
                iterations,
                () => { ran += string.Format("daily/{0}/apis/{1}/operations/{2}/test", x.First, x.Second, x.Third).Length; });

            CodeTimer.Time(
                true,
                "string.Format with explicit .ToString()",
                iterations,
                () =>
            {
                ran +=
                    string.Format("daily/{0}/apis/{1}/operations/{2}/test", x.First.ToString(CultureInfo.InvariantCulture), x.Second.ToString(CultureInfo.InvariantCulture), x.Third.ToString(CultureInfo.InvariantCulture))
                    .Length;
            });

            CodeTimer.Time(
                true,
                "StringBuilder",
                iterations,
                () =>
            {
                StringBuilder sb = new StringBuilder()
                                   .Append("daily/")
                                   .Append(x.First)
                                   .Append("/apis/")
                                   .Append(x.Second)
                                   .Append("/operations/")
                                   .Append(x.Third)
                                   .Append("/test");

                ran += sb.ToString().Length;
            });

            Console.WriteLine(ran);

            return(ran);
        }