计算方法执行时间

/// <summary>
    /// 方法帮助类
    /// </summary>
    public class ActionHelper
    {
        /// <summary>
        /// 统一计算执行时间
        /// </summary>
        /// <param name="action">执行方法</param>
        /// <returns></returns>
        public static int ComputeMethodCostMilliseconds(Action action)
        {
            var stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start(); //  开始监视
            action();
            stopwatch.Stop(); //  停止监视
            var timeSpan = stopwatch.Elapsed; //  获取总时间

            return (int)timeSpan.TotalMilliseconds;
        }
    }
原文地址:https://www.cnblogs.com/zhshlimi/p/8059004.html