• 学习笔记: 泛型


    List<object> 与 List<T> 区别

    object类型参数有2个问题:
    1 装箱拆箱,性能损耗
       传入一个int值(栈)
       object又在堆里面,如果把int传递进来,就会把值从栈里面copy到堆里
       使用的时候,又需要用对象值,又会copy到栈(拆箱)

    2 类型安全问题,可能会有,因为传递的对象是没有限制的

    image

    泛型方法与普通方法性能对比

    public class Monitor
    {
         public static void Show()
         {
             Console.WriteLine("****************Monitor******************");
             {
                 int iValue = 12345;
                 long commonSecond = 0;
                 long objectSecond = 0;
                 long genericSecond = 0;

                {
                     Stopwatch watch = new Stopwatch();
                     watch.Start();
                     for (int i = 0; i < 100_000_000; i++)
                     {
                         ShowInt(iValue);
                     }
                     watch.Stop();
                     commonSecond = watch.ElapsedMilliseconds;
                 }
                 {
                     Stopwatch watch = new Stopwatch();
                     watch.Start();
                     for (int i = 0; i < 100_000_000; i++)
                     {
                         ShowObject(iValue);
                     }
                     watch.Stop();
                     objectSecond = watch.ElapsedMilliseconds;
                 }
                 {
                     Stopwatch watch = new Stopwatch();
                     watch.Start();
                     for (int i = 0; i < 100_000_000; i++)
                     {
                         Show<int>(iValue);
                     }
                     watch.Stop();
                     genericSecond = watch.ElapsedMilliseconds;
                 }
                 Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}"
                     , commonSecond, objectSecond, genericSecond);
             }
         }

        #region PrivateMethod
         private static void ShowInt(int iParameter)
         {
             //do nothing
         }
         private static void ShowObject(object oParameter)
         {
             //do nothing
         }
         private static void Show<T>(T tParameter)
         {
             //do nothing
         }
         #endregion

    }

    image

    泛型方法与普通方法性能一致,还能一个方法满足多个不同类型

    object方法性能低: int 与 object 频繁装箱拆箱


    泛型缓存

    /// <summary>
    /// 每个不同的T,都会生成一份不同的副本
    /// 适合不同类型,需要缓存一份数据的场景,效率高
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class GenericCache<T>
    {
         static GenericCache()
         {
             Console.WriteLine("This is GenericCache 静态构造函数");
             _TypeTime = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));
         }

        private static string _TypeTime = "";

        public static string GetCache()
         {
             return _TypeTime;
         }
         //common<int>(1)
    }

  • 相关阅读:
    Kafka Producer 的缓冲池机制【转】
    【转】kafka如何实现每秒几十万的高并发写入?
    【转】zk和eureka的区别(CAP原则)
    【转】10倍请求压力来袭,你的系统会被击垮吗?
    (转发)深度学习模型压缩与加速理论与实战(一):模型剪枝
    Time Lens: Event-based Video Frame Interpolation
    PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
    Self-Supervised Learning with Swin Transformers
    DashNet: A Hybrid Artificial and Spiking Neural Network for High-speed Object Tracking
    KAIST Multispectral Pedestrian Detection Benchmark
  • 原文地址:https://www.cnblogs.com/xtxtx/p/10664988.html
Copyright © 2020-2023  润新知