Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Dictionary <string, string> source = new Dictionary <string, string>();

            source.Add("format", "1");
            source.Add("key", "123");
            source.Add("accept-language", "ua");
            source.Add("addressdetails", "sdf4 sdgfdsf");
            source.Add("namedetails", "detail");
            source.Add("extratags", "ext");
            source.Add("email", "email");

            string sc = StringFormatExtensions.NamedFormat("https:\\nomi", source);

            Console.WriteLine(sc);

            //Console.ReadLine();
        }
        private void PerformanceComparison(bool parallel, int samplesNo)
        {
            GC.Collect();
            //var m1 = dotMemory.Check();

            long memory0 = GC.GetTotalMemory(true);
            var  s       = "{0}{1}{2}{3}";

            Stopwatch pooled = Stopwatch.StartNew();

            if (parallel)
            {
                Parallel.For(0,
                             samplesNo,
                             i =>
                {
                    // ReSharper disable once UnusedVariable
                    string formatted = "{0}{1}{2}{3}".Format(i + 1, i + 2, i + 3, i + 4);
                });
            }
            else
            {
                for (var i = 0; i < samplesNo; i++)
                {
                    // ReSharper disable once UnusedVariable
                    string formatted = StringFormatExtensions.Format(s, "i + 1", "i + 2", "i + 3", "i + 4");

                    //if (i % 10000 == 0)
                    //    Console.WriteLine("      -- MEMORY DIFF: {0}", GC.GetTotalMemory(false) - memory0);
                }
            }

            pooled.Stop();
            GC.Collect();

            long memory1 = GC.GetTotalMemory(true);


            //long dead1 = 0;
            // dotMemory.Check(m=>
            // {
            //     dead1 = m.GetDifference(m1)
            //             .GetDeadObjects()
            //             .SizeInBytes;
            // });

            // ReSharper disable once HeapView.BoxingAllocation
            Console.WriteLine("{0}variable.Format()      : {1}ms    MEMORY-ALLOCATION: {2}  DEAD: {3}b",
                              parallel ? "PARALLEL " : "",
                              pooled.ElapsedMilliseconds.ToString(),
                              (memory1 - memory0).ToString(),
                              ""//dead1.ToString()
                              );

            //var m2 = dotMemory.Check();

            memory1 = GC.GetTotalMemory(true);

            Stopwatch ordinary = Stopwatch.StartNew();

            if (parallel)
            {
                Parallel.For(0,
                             samplesNo,
                             i =>
                {
                    // ReSharper disable once UnusedVariable
                    // ReSharper disable once UseStringInterpolation
                    string formatted = string.Format("{0}{1}{2}{3}", i + 1, i + 2, i + 3, i + 4);

                    //if (i % 10000 == 0)
                    //    Console.WriteLine("      -- MEMORY DIFF: {0}", GC.GetTotalMemory(false) - memory1);
                });
            }
            else
            {
                for (var i = 0; i < samplesNo; i++)
                {
                    // ReSharper disable once UnusedVariable
                    // ReSharper disable once UseStringInterpolation
                    string formatted = string.Format("{0}{1}{2}{3}", i + 1, i + 2, i + 3, i + 4);

                    //if (i % 10000 == 0)
                    //    Console.WriteLine("      -- MEMORY DIFF: {0}", GC.GetTotalMemory(false) - memory1);
                }
            }
            ordinary.Stop();

            long memory2 = GC.GetTotalMemory(false);

            //long dead2 = 0;
            //dotMemory.Check(m =>
            //{
            //    dead2 = m.GetDifference(m2)
            //            .GetDeadObjects()
            //            .SizeInBytes;
            //});

            Console.WriteLine("{0}String.Format(variable): {1}ms    MEMORY-ALLOCATION: {2} DEAD: {3}b",
                              parallel ? "PARALLEL " : "",
                              ordinary.ElapsedMilliseconds,
                              memory2 - memory1,
                              ""//dead2
                              );
        }