.Net Core 和 .Net Framework 对比01

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口和类
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            const int seed = int.MaxValue;

            Children children = new Children();
            IChildern childern1 = new Children();

            Achdren abChildren = new Achdren();
            AbChildren abChildren1 = new Achdren();

            for (int j = 0; j < 10; j++)
            {
                stopwatch.Restart();
                for (int i = 0; i < seed; i++)
                {
                    children.Do();

                }
                Console.WriteLine($"Children直接调用{stopwatch.ElapsedMilliseconds}");
                stopwatch.Restart();
                for (int i = 0; i < seed; i++)
                {
                    childern1.Do();

                }
                Console.WriteLine($"IChildern接口调用{stopwatch.ElapsedMilliseconds}");
                stopwatch.Restart();

                stopwatch.Start();
                for (int i = 0; i < seed; i++)
                {
                    abChildren.Do();

                }
                Console.WriteLine($"Achdren直接调用{stopwatch.ElapsedMilliseconds}");
                stopwatch.Restart();
                for (int i = 0; i < seed; i++)
                {
                    abChildren1.Do();
                   
                }
                Console.WriteLine($"AbChildren基类接口{stopwatch.ElapsedMilliseconds}");
               // stopwatch.Restart();
                Console.WriteLine($"\n");
            }
            Console.ReadLine();
        }
    }



    public interface IChildern
    {
        void Do();
    }
    public class Children : IChildern
    {
        public void Do()
        {

        }
    }


    public abstract class AbChildren
    {
        public  abstract  void Do();
    }
    public class Achdren : AbChildren
    {
        public override void Do()
        {
            
        }

    }

}

 

同一段代码在 core 和framework 下执行对比,速度快了将近5倍

 

posted @ 2020-04-25 20:11  谁说程序猿很猥琐  阅读(548)  评论(0编辑  收藏  举报