C# 原生参数方法,object参数方法,泛型参数方法 性能比较

class Program
{
    static void Main(string[] args)
    {//转载请保留 http://www.luofenming.com/show.aspx?id=ART2020012600001
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for(int i = 0; i < 100000000; i++)
            {
                ShowInt(1);//原生方法
            }
            stopwatch.Stop();
            Console.WriteLine($"ShowInt耗时{stopwatch.ElapsedMilliseconds}");
        }
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 100000000; i++)
            {
                ShowObject(1);//object方法
            }
            stopwatch.Stop();
            Console.WriteLine($"ShowObject耗时{stopwatch.ElapsedMilliseconds}");
        }
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 100000000; i++)
            {
                Show(1);//泛型方法
            }
            stopwatch.Stop();
            Console.WriteLine($"Show耗时{stopwatch.ElapsedMilliseconds}");
        }

    }
    
    static void ShowInt(int i)
    {

    }
    static void ShowObject(object o)
    {

    }
    static void Show<T>(T t)
    {

    }
}

运行结果

评论