加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在从C转换为C#的代码中,我应该使用什么而不是memcpy?

发布时间:2020-12-16 05:04:52 所属栏目:百科 来源:网络整理
导读:这条线在这里: memcpy(v[0],b_y1[0],160U * sizeof(double)); 其中v和b_y1区域是双数组(double []). 这条线到底在做什么?它是通过将MatLab转换为C到C#生成的.如果需要,我可以提供以下全部功能. 解决方法 有很多方法可以做到这一点.如果您选择并使用memcpy,
这条线在这里:
memcpy(v[0],b_y1[0],160U * sizeof(double));

其中v和b_y1区域是双数组(double []).

这条线到底在做什么?它是通过将MatLab转换为C到C#生成的.如果需要,我可以提供以下全部功能.

解决方法

有很多方法可以做到这一点.如果您选择并使用memcpy,请导入Dll

不过你也可以用

> Buffer.BlockCopy

Copies a specified number of bytes from a source array starting at a
particular offset to a destination array starting at a particular
offset.

> Array.Copy

Copies a range of elements in one Array to another Array and performs
type casting and boxing as required.

>手动复制逐项清单
>甚至更快就是使用,固定和不安全以及指针

但是,如果你真的想要速度,Array.Copy和Buffer.BlockCopy可能是你最快和最接近的匹配

基准

只因为我很烦人,我打开了基准测试程序.比赛你的马!

----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
Description      : Intel64 Family 6 Model 58 Stepping 9
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3901 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

Total Benchmarks : Inputs (1) * Scales (7) * Benchmarks (4) * Runs (500) = 14,000

结果1

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------

Total Benchmarks : Inputs (1) * Scales (7) * Benchmarks (4) * Runs (500) = 14,000

NET Framework 4.7.1

--- Standard input --------------------------------------------------------------------
| Value           |   Average |   Fastest |    Cycles |    Garbage | Test |      Gain |
--- Scale 10 ----------------------------------------------------------- Time 0.994 ---
| ArrayCopy       |  0.005 ms |  0.004 ms |  23.016 K |   7.927 KB | N/A  |   14.98 % |
| ElemCopy        |  0.006 ms |  0.004 ms |  23.591 K |   8.000 KB | N/A  |   12.08 % |
| ElemCopy Unsafe |  0.006 ms |  0.004 ms |  26.426 K |   7.915 KB | N/A  |    0.77 % |
| BlockCopy       |  0.006 ms |  0.004 ms |  26.295 K |   7.836 KB | Base |    0.00 % |
--- Scale 100 ---------------------------------------------------------- Time 1.124 ---
| ArrayCopy       |  0.005 ms |  0.004 ms |  20.581 K |   7.937 KB | N/A  |    1.60 % |
| BlockCopy       |  0.005 ms |  0.004 ms |  20.915 K |   8.000 KB | Base |    0.00 % |
| ElemCopy        |  0.005 ms |  0.004 ms |  21.836 K |   8.000 KB | N/A  |   -4.15 % |
| ElemCopy Unsafe |  0.005 ms |  0.004 ms |  22.357 K |   7.970 KB | N/A  |  -10.85 % |
--- Scale 1,000 -------------------------------------------------------- Time 1.322 ---
| ArrayCopy       |  0.005 ms |  0.005 ms |  23.106 K |   8.000 KB | N/A  |    6.31 % |
| ElemCopy Unsafe |  0.006 ms |  0.005 ms |  24.075 K |   8.000 KB | N/A  |    1.41 % |
| ElemCopy        |  0.006 ms |  0.005 ms |  24.392 K |   8.000 KB | N/A  |    0.46 % |
| BlockCopy       |  0.006 ms |  0.004 ms |  24.766 K |   8.015 KB | Base |    0.00 % |
--- Scale 10,000 ------------------------------------------------------- Time 1.727 ---
| BlockCopy       |  0.013 ms |  0.009 ms |  51.749 K |  86.172 KB | Base |    0.00 % |
| ArrayCopy       |  0.016 ms |  0.014 ms |  61.467 K |  86.172 KB | N/A  |  -23.61 % |
| ElemCopy Unsafe |  0.017 ms |  0.015 ms |  63.659 K |  86.172 KB | N/A  |  -28.22 % |
| ElemCopy        |  0.019 ms |  0.016 ms |  70.479 K |  86.172 KB | N/A  |  -41.93 % |
--- Scale 100,000 ------------------------------------------------------ Time 1.825 ---
| BlockCopy       |  0.050 ms |  0.045 ms | 178.829 K | 789.273 KB | Base |    0.00 % |
| ArrayCopy       |  0.101 ms |  0.089 ms | 357.518 K | 789.273 KB | N/A  | -102.24 % |
| ElemCopy Unsafe |  0.121 ms |  0.108 ms | 428.179 K | 789.273 KB | N/A  | -143.35 % |
| ElemCopy        |  0.133 ms |  0.118 ms | 469.168 K | 789.273 KB | N/A  | -166.41 % |
--- Scale 1,000,000 ---------------------------------------------------- Time 4.946 ---
| BlockCopy       |  0.494 ms |  0.409 ms |   1.730 M |   7.637 MB | Base |    0.00 % |
| ElemCopy Unsafe |  1.336 ms |  1.164 ms |   4.674 M |   7.637 MB | N/A  | -170.59 % |
| ArrayCopy       |  1.478 ms |  1.298 ms |   5.169 M |   7.637 MB | N/A  | -199.40 % |
| ElemCopy        |  1.910 ms |  1.607 ms |   6.675 M |   7.637 MB | N/A  | -286.95 % |
--- Scale 10,000 -------------------------------------------------- Time 31.376 ---
| BlockCopy       |  5.408 ms |  4.589 ms |  18.896 M |  76.302 MB | Base |    0.00 % |
| ElemCopy Unsafe | 43.981 ms | 35.344 ms | 153.137 M |  76.302 MB | N/A  | -713.21 % |
| ElemCopy        | 46.318 ms | 37.623 ms | 161.225 M |  76.302 MB | N/A  | -756.43 % |
| ArrayCopy       | 48.171 ms | 38.471 ms | 167.548 M |  76.302 MB | N/A  | -790.69 % |
---------------------------------------------------------------------------------------

结果2

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .Net Core 2.0 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------

Total Benchmarks : Inputs (1) * Scales (7) * Benchmarks (4) * Runs (500) = 14,000

.Net Core 2.0

测试1

--- Standard input ----------------------------------------------------------------------
| Value           |   Average |   Fastest |    Cycles |    Garbage | Test |        Gain |
--- Scale 10 ------------------------------------------------------------- Time 1.221 ---
| ElemCopy Unsafe |  0.006 ms |  0.003 ms |  23.940 K |   8.000 KB | N/A  |     13.53 % |
| BlockCopy       |  0.007 ms |  0.004 ms |  27.573 K |   7.923 KB | Base |      0.00 % |
| ArrayCopy       |  0.007 ms |  0.004 ms |  28.341 K |   7.914 KB | N/A  |     -5.98 % |
| ElemCopy        |  0.007 ms |  0.003 ms |  28.939 K |   7.914 KB | N/A  |     -6.12 % |
--- Scale 100 ------------------------------------------------------------ Time 1.333 ---
| BlockCopy       |  0.005 ms |  0.004 ms |  19.855 K |   7.970 KB | Base |      0.00 % |
| ElemCopy        |  0.005 ms |  0.004 ms |  22.061 K |   7.950 KB | N/A  |    -11.44 % |
| ArrayCopy       |  0.005 ms |  0.004 ms |  22.793 K |   8.000 KB | N/A  |    -15.94 % |
| ElemCopy Unsafe |  0.006 ms |  0.004 ms |  23.715 K |   7.999 KB | N/A  |    -21.65 % |
--- Scale 1,000 ---------------------------------------------------------- Time 1.464 ---
| BlockCopy       |  0.005 ms |  0.004 ms |  21.045 K |   8.001 KB | Base |      0.00 % |
| ElemCopy Unsafe |  0.005 ms |  0.004 ms |  21.731 K |   8.016 KB | N/A  |     -5.53 % |
| ElemCopy        |  0.006 ms |  0.004 ms |  24.120 K |   8.000 KB | N/A  |    -17.49 % |
| ArrayCopy       |  0.006 ms |  0.004 ms |  27.113 K |   8.013 KB | N/A  |    -31.62 % |
--- Scale 10,000 --------------------------------------------------------- Time 1.846 ---
| BlockCopy       |  0.010 ms |  0.008 ms |  37.962 K |  86.172 KB | Base |      0.00 % |
| ArrayCopy       |  0.018 ms |  0.014 ms |  67.134 K |  86.172 KB | N/A  |    -83.72 % |
| ElemCopy Unsafe |  0.019 ms |  0.014 ms |  72.097 K |  86.172 KB | N/A  |    -99.35 % |
| ElemCopy        |  0.021 ms |  0.016 ms |  77.657 K |  86.172 KB | N/A  |   -113.22 % |
--- Scale 100,000 -------------------------------------------------------- Time 2.880 ---
| BlockCopy       |  0.027 ms |  0.020 ms | 100.355 K | 789.305 KB | Base |      0.00 % |
| ArrayCopy       |  0.385 ms |  0.289 ms |   1.346 M | 789.305 KB | N/A  | -1,305.09 % |
| ElemCopy Unsafe |  0.404 ms |  0.280 ms |   1.414 M | 789.305 KB | N/A  | -1,374.97 % |
| ElemCopy        |  0.408 ms |  0.322 ms |   1.424 M | 789.305 KB | N/A  | -1,389.61 % |
--- Scale 1,000 ----------------------------------------------------- Time 11.892 ---
| BlockCopy       |  0.632 ms |  0.415 ms |   2.198 M |   7.637 MB | Base |      0.00 % |
| ElemCopy        |  4.645 ms |  3.347 ms |  16.159 M |   7.637 MB | N/A  |   -635.36 % |
| ArrayCopy       |  4.706 ms |  3.684 ms |  16.376 M |   7.637 MB | N/A  |   -645.01 % |
| ElemCopy Unsafe |  4.774 ms |  3.467 ms |  16.568 M |   7.637 MB | N/A  |   -655.66 % |
--- Scale 10,000 ---------------------------------------------------- Time 35.806 ---
| BlockCopy       |  6.116 ms |  4.635 ms |  21.294 M |  76.302 MB | Base |      0.00 % |
| ElemCopy Unsafe | 44.132 ms | 35.039 ms | 153.807 M |  76.302 MB | N/A  |   -621.63 % |
| ArrayCopy       | 49.990 ms | 40.360 ms | 173.860 M |  76.302 MB | N/A  |   -717.41 % |
| ElemCopy        | 50.552 ms | 38.044 ms | 175.432 M |  76.302 MB | N/A  |   -726.60 % |
-----------------------------------------------------------------------------------------

输入

public static double[] ArrayOfDouble(int scale)
{
   return Enumerable.Range(1,scale)
                    .Select(x =>_rand.NextDouble())
                    .ToArray();
}

[Test("BlockCopy","",true)]
public double[] Test1(double[] input,int scale)
{
   var result = new double[scale];
   Buffer.BlockCopy(input,result,input.Length);
   return result;
}

[Test("ArrayCopy",false)]
public  double[] Test2(double[] input,int scale)
{
   var result = new double[scale];
   Array.Copy(input,input.Length);
   return result;
}

[Test("ElemCopy",false)]
public  double[] Test3(double[] input,int scale)
{
   var result = new double[scale];
   for (var i=0; i <input.Length; i++)
      result[i] = input[i];
   return result;
}

[Test("ElemCopy Unsafe",false)]
unsafe public  double[] Test4(double[] input,int scale)
{
   var result = new double[scale];
   fixed(double* pInput = input,pResult = result)
   for (var i=0; i <input.Length; i++)
      *(pResult+i) = *(pResult+i);
   return result;
}

摘要

这应该是一粒盐,任何真正的测试都应该在你自己的系统上进行.然而,这是对我缩放时得到的内容的一个很好的估计

在这种情况下,使用双精度BlockCopy执行得非常好,ArrayCopy在高规模下降,我不知道为什么,Unsafe在这些事情上始终具有性能优势,但无法与专用的BCL方法相匹配.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读