当前位置: 首页 > news >正文

【C#】关于Array.Copy 和 GC

关于Array.Copy 和 GC

	//一个简单的 数组copy   什么情况下会触发GC呢[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]public static void Copy(Array sourceArray,long sourceIndex,Array destinationArray,long destinationIndex,long length);

当源和目标的类型不一致,由小转大,比如由byte 到 short ,int 都会触发GC ,我不知道内部机制如何,可能是拆装箱导致的 ,不确定,不过在实际开发中确实出现了这种问题,所以使用的时候 类型要匹配

贴一段测试代码

   void Update(){var start = DateTime.Now;for (int i = 0; i < this.inputData.Length; i++){this.inputData[i] = -125;var a = Mathf.Abs(this.inputData[i]);a = Mathf.Clamp(a, 0, 1023);this.inputData[i] = (short)(a * 255 / 1023);}Debug.Log($"<color=#ff00ff>CPU cost : {(DateTime.Now - start).TotalMilliseconds}</color>");start = DateTime.Now;**byte[] outData = new byte[size];**this.ComputeData(ref outData); //GPUint[] outData2 = new int[this.size];Array.Copy(outData, 0, outData2, 0, outData.Length);Debug.Log($"<color=#ffff00>GPU cost : {(DateTime.Now - start).TotalMilliseconds}</color>");}private void ComputeData(ref int[] outPutBytes){//if (this.inputbuffer == null){inputbuffer = new ComputeBuffer(this.inputData.Length, 4); //定义缓冲区(参数:数组长度、每个数组元素占用字节数)}//if (this.outputbuffer == null){outputbuffer = new ComputeBuffer(this.outputData.Length, 4);}inputbuffer.SetData(this.inputData); //待计算数据加载入缓冲区this.shader.SetBuffer(this.k, "inputData", inputbuffer); //定义输入口this.shader.SetBuffer(this.k, "outputData", outputbuffer); //定义输出口this.shader.Dispatch(this.k, this.inputData.Length / 1024, 1, 1); //开始执行(参数:主函数下标、线程组的XYZ个数)outputbuffer.GetData(this.outputData); //缓冲区获得计算好的数据for (int i = 0; i < this.outputData.Length; i++){outPutBytes[i] = (byte)this.outputData[i];}inputbuffer.Dispose(); //清除缓存outputbuffer.Dispose();}

运行情况
在这里插入图片描述
GC

在这里插入图片描述
恐怖如斯啊~~~

http://www.lryc.cn/news/162287.html

相关文章:

  • Vue前端框架08 Vue框架简介、VueAPI风格、模板语法、事件处理、数组变化侦测
  • WebStorm使用PlantUML
  • Python做批处理,给安卓设备安装应用和传输图片
  • 如何获取springboot中所有的bean
  • 大数据技术之Hadoop:HDFS存储原理篇(五)
  • 用C语言实现牛顿摆控制台动画
  • 如何自己开发一个前端监控SDK
  • node.js笔记
  • mysql 增量备份与恢复使用详解
  • 9月5日上课内容 第一章 NoSQL之Redis配置与优化
  • QT 第四天
  • nrf52832 GPIO输入输出设置
  • MyBatis 动态 SQL 实践教程
  • CSS 斜条纹进度条
  • JavaScript(1)每天10个小知识点
  • scanf和scanf_s函数详解
  • 基于SSM的在线购物系统
  • 认识JVM的内存模型
  • Java8实战-总结19
  • 论文浅尝 | 训练语言模型遵循人类反馈的指令
  • 【云计算网络安全】解析DDoS攻击:工作原理、识别和防御策略 | 文末送书
  • 64位Linux系统上安装64位Oracle10gR2及Oracle11g所需的依赖包
  • Unity InputSystem 基础使用之鼠标交互
  • 《算法竞赛·快冲300题》每日一题:“二进制数独”
  • CnosDB 签约京清能源,助力分布式光伏发电解决监测系统难题。
  • 汇编:lea 需要注意的一点
  • SQL语言的分类:DDL(数据库、表的增、删、改)、DML(数据的增、删、改)
  • 微信小程序精准扶贫数据收集小程序平台设计与实现
  • PostgreSQL 流复制搭建
  • 机器学习笔记之最优化理论与方法(十)无约束优化问题——共轭梯度法背景介绍