• 打造自己的性能测试类


       之前做性能测试老是零零散散的一个 Stopwatch,又一个Stopwatch ,去测试 方法的执行效率,从而进行验证。

              为了方便使用,自己写一个好用的 性能测试类,目的在于方便好用,点到即可。

       使用方式(如下,测试 NameValueCollection 与 Dictionary 的 性能):

    static void Main(string[] args)
            {
                Console.WriteLine(new Action(() => {
    
                    NameValueCollection nvc = new NameValueCollection();
                    for (int j = 0; j < 10; j++)
                    {
                        nvc.Add(j.ToString(), j.ToString());
                    }
    
                    for (int j = 0; j < 10; j++)
                    {
                        string temp = nvc[j.ToString()].ToString();
                    }
    
                }).ForWatchMS(100000));
    
    
                Console.WriteLine(new Action(() => {
                    Dictionary<string, string> nvc = new Dictionary<string, string>();
                    for (int j = 0; j < 10; j++)
                    {
                        nvc.Add(j.ToString(), j.ToString());
                    }
    
                    for (int j = 0; j < 10; j++)
                    {
                        string temp = nvc[j.ToString()].ToString();
                    }
                }).ForWatchMS(100000));
                
            }

             代码如下:

      

    using System;
    using System.Diagnostics;
    
    public static class SpendTimeHelper
    {
        #region Action
            
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS(this Action act, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                act.Invoke();
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T>(this Action<T> act, T t, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                act.Invoke(t);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2>(this Action<T1, T2> act, T1 t1, T2 t2, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < forCount; i++)
            {
                act.Invoke(t1, t2);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3>(this Action<T1, T2, T3> act, T1 t1, T2 t2, T3 t3, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                act.Invoke(t1, t2, t3);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3, T4>(this Action<T1, T2, T3, T4> act, T1 t1, T2 t2, T3 t3, T4 t4, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                act.Invoke(t1, t2, t3, t4);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="act"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3, T4, T5>(this Action<T1, T2, T3, T4, T5> act, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                act.Invoke(t1, t2, t3, t4, t5);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
    
        #endregion
    
        #region Func
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T>(this Func<T> fun, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke();
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, TResult>(this Func<T1, TResult> fun, T1 t1, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke(t1);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, TResult>(this Func<T1, T2, TResult> fun, T1 t1, T2 t2, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke(t1, t2);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3, TResult>(this Func<T1, T2, T3, TResult> fun, T1 t1, T2 t2, T3 t3, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke(t1, t2, t3);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3, T4, TResult>(this Func<T1, T2, T3, T4, TResult> fun, T1 t1, T2 t2, T3 t3, T4 t4, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke(t1, t2, t3, t4);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
        /// <summary>
        /// 执行forCount次的花费时间(ms) 
        /// </summary>
        /// <param name="fun"></param>
        /// <param name="forCount">执行次数</param>
        /// <returns>花费时间:毫秒</returns>
        public static long ForWatchMS<T1, T2, T3, T4, T5, TResult>(this Func<T1, T2, T3, T4, T5, TResult> fun, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, long forCount)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            for (int j = 0; j < forCount; j++)
            {
                fun.Invoke(t1, t2, t3, t4, t5);
            }
            watch.Stop();
            return watch.ElapsedMilliseconds;
        }
    
    
    
        #endregion
    }
  • 相关阅读:
    Timer Pattern
    la négation
    expression de la fréquence
    .NET 索引器
    JQuery.Gantt(甘特图) 开发指南
    .NET 预处理器指令
    .NET 数据类型之匿名类型(var)
    .NET base与this
    .NET using关键字
    .NET 基础语句
  • 原文地址:https://www.cnblogs.com/lztkdr/p/SpendTimeHelper.html
Copyright © 2020-2023  润新知