NSoft.NFramework.With.OperationTimer C# (CSharp) Method

OperationTimer() public static method

지정된 함수를 수행하는데 걸리는 시간 (msec)을 검사한다.
public static OperationTimer ( System.Action action, bool gabageCollect = false ) : double
action System.Action 시간 측정이 필요한 함수
gabageCollect bool 시간측정을 더 정밀하게 하기 위해 GabageCollect()를 수행하고 할 것인가?
return double
        public static double OperationTimer(Action action, bool gabageCollect = false) {
            action.ShouldNotBeNull("action");

            if(gabageCollect)
                GabageCollect();

            var stopwatch = new Stopwatch();

            try {
                stopwatch.Start();

                action();
            }
            finally {
                stopwatch.Stop();
            }

            return stopwatch.ElapsedMilliseconds;
        }

Usage Example

        public void CallsDelegateAndReturnNonZeroTiming()
        {
            bool   called = false;
            double msecs  = With.OperationTimer(delegate {
                called = true;
                ThreadTool.Sleep(40);
            });

            // The only reliable way of testing this that I can think of.
            Assert.IsTrue(msecs > 0);
            Assert.IsTrue(called);

            Console.WriteLine("Operation elapsed time (msec): " + msecs);
        }