zoukankan      html  css  js  c++  java
  • .net core 与 .net framework 的一些不同点

      随大流的用.net core也没有系统的学习,只知道.net core支持跨平台,其他的点上没办法讲出个一二,今天听老师讲到的一个点,记录下来

      官网介绍:https://docs.microsoft.com/zh-cn/dotnet/core/about

      测试点:在.net core/.net framework不同平台下 接口,抽象类,类以及各自对应的实现类 循环调用,哪个平台中哪个的运行效率最高?  

      新建两个控制台程序,.net core(3.1) 和 .net framework(我用的是4.7.2)   

             

        

       同样一份代码,在不同平台运行,以下是code:

     1     class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5 
     6             Console.WriteLine("测试不同框架下,接口与抽象类的效率:.net core 3.1 release: ");
     7             var t = new Test();
     8             t.PerformanceTest();
     9             Console.ReadKey();
    10         }
    11     }
    View Code
      1 namespace CoreConsole
      2 {
      3     public class Test
      4     {
      5         public void PerformanceTest()
      6         {
      7 
      8             IChild record1 = new IChild();
      9             IBase record2 = record1;
     10 
     11             AbsChild row1 = new AbsChild();
     12             AbsBaseClass row2 = row1;
     13 
     14             Child result1 = new Child();
     15             Base result2 = result1;
     16 
     17             Console.WriteLine($"循环{int.MaxValue}次,去执行接口,抽象类,类以及实现类里面的方法");
     18 
     19             Stopwatch stopwatch = new Stopwatch();
     20 
     21               
     22             stopwatch.Restart();
     23             for (int i = 0; i < int.MaxValue; i++)
     24             {
     25                 record1.Do();
     26             }
     27             Console.WriteLine($"接口实现类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     28 
     29 
     30             stopwatch.Restart();
     31             for (int i = 0; i < int.MaxValue; i++)
     32             {
     33                 record2.Do();
     34             }
     35             Console.WriteLine($"接口基类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     36 
     37 
     38 
     39             stopwatch.Restart();
     40             for (int i = 0; i < int.MaxValue; i++)
     41             {
     42                 row1.Do();
     43             }
     44             Console.WriteLine($"抽象实现类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     45 
     46 
     47             stopwatch.Restart();
     48             for (int i = 0; i < int.MaxValue; i++)
     49             {
     50                 row2.Do();
     51             }
     52             Console.WriteLine($"抽象基类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     53 
     54 
     55 
     56             stopwatch.Restart();
     57             for (int i = 0; i < int.MaxValue; i++)
     58             {
     59                 result1.Do();
     60             }
     61             Console.WriteLine($"类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     62 
     63             stopwatch.Restart();
     64             for (int i = 0; i < int.MaxValue; i++)
     65             {
     66                 result2.Do();
     67             }
     68             Console.WriteLine($"基类调用时间:{stopwatch.ElapsedMilliseconds} 毫秒");
     69 
     70         }
     71     }
     72      
     73     public interface IBase
     74     {
     75         void Do();
     76     }
     77 
     78     public class IChild : IBase
     79     {
     80         public void Do()
     81         {
     82              
     83         }
     84     }
     85 
     86     public abstract class AbsBaseClass
     87     {
     88         public abstract void Do();
     89     }
     90 
     91     public class AbsChild : AbsBaseClass
     92     {
     93         public override void Do()
     94         {
     95         }
     96     }
     97 
     98     public class Base
     99     {
    100       public  virtual void Do() { }
    101     }
    102 
    103     public class Child : Base
    104     {
    105         public override void Do()
    106         {
    107             
    108         }
    109     }
    110 }
    View Code

        下图是运行的效果对比图:

        总体测试下来是.net core > .net framework   且快10倍左右 (我看老师运行快个7,8倍左右) 

     接口和抽象谁效率高?我将test的方法循环了10次,:.net core中不相上下,抽象类  > 接口 (略微)

       个人经验:在接口和抽象类的选择中,最终还是看业务需求~ 共性大就用接口(生物:人,鱼,狗),特性就用抽象~(鱼:鲫鱼,草鱼,鲤鱼)

     初学,有什么错误,欢迎指正~

       最后一点,谁能指出为什么两个平台差异这么大呢?

        

        

  • 相关阅读:
    重新定位Excel Addin插件的方法
    VBA调用DOS程序两种方法
    Simulink Memory vs Unit Delay
    C#对象序列化与反序列化zz
    [leetcode]Sqrt(x) @ Python
    [leetcode]Sort Colors @ Python
    [leetcode]Pow(x, n) @ Python
    [leetcode]Edit Distance @ Python
    [leetcode]Rotate Image @ Python
    [leetcode]Length of Last Word @ Python
  • 原文地址:https://www.cnblogs.com/hanliping/p/12244053.html
Copyright © 2011-2022 走看看