openHistorian.DebugStopwatch.TimeEvent C# (CSharp) Метод

TimeEvent() публичный Метод

public TimeEvent ( System.Action function ) : double
function System.Action
Результат double
        public double TimeEvent(Action function)
        {
            sw.Reset();
            GC.Collect();
            function();
            int count = 0;
            while (sw.Elapsed.TotalSeconds < .25)
            {
                sw.Start();
                function();
                sw.Stop();
                count++;
            }
            return sw.Elapsed.TotalSeconds / count;
        }
    }

Usage Example

        public void Test1()
        {
            MemoryPoolTest.TestMemoryLeak();
            DebugStopwatch sw = new DebugStopwatch();

            for (int max = 10; max < 10000; max *= 2)
            {
                Action add1 = () =>
                    {
                        SortedList<int, int> list = new SortedList<int, int>();
                        for (int x = 0; x < max; x++)
                        {
                            list.Add(x, x);
                        }
                    };

                Action add2 = () =>
                    {
                        List<int> keys = new List<int>(max);
                        List<int> values = new List<int>(max);

                        for (int x = 0; x < max; x++)
                        {
                            keys.Add(x);
                            values.Add(x);
                        }

                        var sl = SortedListConstructor.Create(keys, values);

                    };

                //var makeList = new SortedListConstructorUnsafe<int, int>();
                //Action add3 = () =>
                //{
                //    List<int> keys = new List<int>(max);
                //    List<int> values = new List<int>(max);

                //    for (int x = 0; x < max; x++)
                //    {
                //        keys.Add(x);
                //        values.Add(x);
                //    }

                //    var sl = makeList.Create(keys, values);
                //    //var sl = SortedListConstructor.CreateUnsafe(keys, values);

                //};
                System.Console.WriteLine("Old Method " + max + " " + sw.TimeEvent(add1) * 1000000);
                System.Console.WriteLine("New Method " + max + " " + sw.TimeEvent(add2) * 1000000);
                //Console.WriteLine("Unsafe Method " + max + " " + sw.TimeEvent(add3) * 1000000);
                MemoryPoolTest.TestMemoryLeak();
            }


        }
All Usage Examples Of openHistorian.DebugStopwatch::TimeEvent