C# unsafe 快速复制数组
(1)
/// <summary>
/// 复制内存
/// </summary>
/// <param name="dest">目标指针位置</param>
/// <param name="src">源指针位置</param>
/// <param name="count">字节长度</param>
/// <returns></returns>
[DllImport("msvcrt.dll")]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, int count);
unsafe static int[] MyCopy(int[] oriInts)
{
int[] result = new int[oriInts.Length];
int lenth= oriInts.Length;
fixed (int* pOri= oriInts) //fixed语句获取指向任意值类型、任意值类型数组或字符串的指针
{
fixed (int* pResult = result)
{
memcpy(new IntPtr(pResult), new IntPtr(pOri), sizeof(int) * lenth);//注意,第一个参数和第二个参数的顺序
}
}
return result;
}
static int[] MyCopyB(int[] oriInts)
{
int[] result = new int[oriInts.Length];
for(int i=0;i<oriInts.Length;i++)
{
result[i]= oriInts[i];
}
return result;
}
static void Main(string[] args)
{
var a = sizeof(int);
int[] ori = new int[100000000];
for(int i = 0; i < ori.Length; i++)
{
ori[i] = i;
}
Stopwatch sw = new Stopwatch();
sw.Start();
int[] copyA= MyCopy(ori);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
int[] copyB = MyCopyB(ori);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

(2)
static unsafe void MyCopy(double[] source, float[]target,int targetOffset,int count)
{
fixed(double* sourcePtr = source)
{
fixed(float* targetPtr = target)
{
float* targetStPtr = targetPtr + targetOffset;
double* sourceEndPtr = sourcePtr + count;
for(double* iPtr = sourcePtr; iPtr != sourceEndPtr; iPtr+=1)
{
*targetStPtr = (float)*iPtr;
targetStPtr += 1;
}
}
}
}
static void MyCopyB(double[] source, float[] target,int targetOffset,int count)
{
for(int i = 0; i < count; i++)
{
target[targetOffset + i] = (float)source[i];
}
}
static void Main(string[] args)
{
double[] ori = new double[10000000];
for(int i = 0; i < ori.Length; i++)
{
ori[i] = i + 2;
}
Stopwatch sw = new Stopwatch();
float[] copyA = new float[ori.Length];
float[] copyB = new float[ori.Length];
sw.Start();
MyCopy(ori,copyA,0,ori.Length);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
sw.Restart();
MyCopyB(ori,copyB,0,ori.Length);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}

可见第一个基于指针的复制,非常的快。
C# unsafe 快速复制数组的更多相关文章
- System.Properties和System.getenv区别
网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...
- Java集合---Array类源码解析
Java集合---Array类源码解析 ---转自:牛奶.不加糖 一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Prim ...
- 【JAVA常用类演示】
一.java.lang.System类. public final class Systemextends Object 该类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的 ...
- System & Runtime &Math
package com.shushine.framework.第七章Java标准类库;/** * * <p> * 描述该类情况 {@link 代表跟谁有关系} * </p> * ...
- JAVA基础学习day18--常用工具类
一.System 1.1.概述 System 类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的设施中,有标准输入.标准输出和错误输出流:对外部定义的属性和环境变量的访问:加载 ...
- Java集合---Arrays类源码解析
一.Arrays.sort()数组排序 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类型: ...
- 关于JAVA System常见类的一些总结
一.JAVA System类概述 1.概述: System 类是一个抽象类,所有的字段和方法都是静态的,即不能被实例化.其中包含一些有用的类字段和方法,它不能被实例化.在 System 类提供的设施中 ...
- Arrays.sort源代码解析
Java Arrays.sort源代码解析 Java Arrays中提供了对所有类型的排序.其中主要分为Primitive(8种基本类型)和Object两大类. 基本类型:采用调优的快速排序: 对象类 ...
- System.in.read()
用读取键盘输入必须构建 1.输入流 System.in; 2.字符输入流 InputStreamReader 3.缓存输入流 BufferedRead ...
- 黑马程序员_Java其他对象(System,Runtime,Date,Calendar,Marh-Random)
System System类包含一些有用的类字段和方法(都是静态的).它不能被实例化. 在System类提供的设施中,有标准输入.标准输出和错误输出流:对外部定义的属性和环境变量的访问:加载文件和库的 ...
随机推荐
- APEX实战第1篇:本地部署拥有AI能力的APEX
学会部署APEX是为了更好构建企业级AI应用打基础,比如企业级的知识平台.智能报表等. 先前在<手把手教你喂养 DeepSeek 本地模型>,使用AnythingLLM方式,虽然操作上已经 ...
- Winform ShowDialog如何让先前Show的窗体可以交互
背景描述 最近项目中有一个需求,全局有一个共用的窗体,能够打开不同模块的报告,由于需要兼容不同模块,代码复杂,启动速度慢.优化方案为将窗体启动时就创建好,需要查看报告时,使用此单例弹窗加载不同模块下的 ...
- 有关算法与数据结构的考题解答参考汇总 [C++] [链表] · 第三篇
早先年考研的主考科目正是[算法与数据结构],复习得还算可以.也在当时[百度知道]上回答了许多相关问题,现把他们一起汇总整理一下,供读者参考. [1] 原题目地址:https://zhidao.baid ...
- 记录:tinyrenderer---1.2 Rasterizing the boundary
光栅化三角形 Scanline rendering(扫描线渲染),一个老式的算法 按y轴坐标进行排序,我这里采取降序,ay > by > cy 同时光栅化三角形的左右两边 绘制水平线段,连 ...
- go gin web服务器使用fvbock/endless优雅地重启或停止
gin使用fvbock/endless gin 正常使用注册路由时: package main import "github.com/gin-gonic/gin" func mai ...
- http://eslint.org/docs/rules/semi
报错: Errors: 88 http://eslint.org/docs/rules/semi 56 http://eslint.org/docs/rules/quotes 34 http://es ...
- numpy -- 处理数值型数据 -- 数据分析三剑客
博客地址:https://www.cnblogs.com/zylyehuo/ NumPy(Numerical Python) 是 Python 语言中做科学计算的基础库.重在于数值计算,也是大部分Py ...
- c#数据库操作ORM映射框架
主要功能介绍 支持Oracle,SQL Server,MySQL,SQLLite等数据库..主要功能: 支持查询返回动态类型Dynamic以及可扩展类型ExpandoDynamic 表拆分,根据某个日 ...
- VMware网络虚拟化介绍(之一)
2014年5月,在我加入VMware三个月之后,我涂鸦了一篇<扒一扒SDN的那些事儿>,当时放言如果阅读量过百就写续篇.后来果然阅读量没过百,也就80多的样子,其中好几份还是我自恋地进去查 ...
- 【Linux】2.2 Linux安装
安装 vm 和 Centos 学习 Linux 需要一个环境,我们需要创建一个虚拟机,然后在虚拟机上安装一个 Centos 系统来学习. 先安装 virtual machine ,vm12 再安装 L ...