在从C ++转换为C#的代码中,应该使用什么代替memcpy?

时间:2018-08-01 01:30:11

标签: c# c++

有问题的行在这里:

memcpy(v[0], b_y1[0], 160U * sizeof(double));

其中vb_y1构成双精度数组(double[])。

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

2 个答案:

答案 0 :(得分:8)

您可以通过多种方式做到这一点。如果您选择并使用memcpy

,则导入Dll

但是您也可以使用

  

从源数组中复制指定数量的字节,起始于a   从特定位置开始到目标数组的特定偏移量   偏移量。

  

将一个数组中的一系列元素复制到另一个数组中并执行   根据需要键入投放和拳击内容。

  • 按建议的方式逐项手动复制项目

  • 使用fixedunsafe以及指针

但是,如果您确实想要速度,Array.CopyBuffer.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,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,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,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, 0, result, 0, input.Length);
   return result;
}

[Test("ArrayCopy", "", false)]
public  double[] Test2(double[] input, int scale)
{
   var result = new double[scale];
   Array.Copy(input, 0, result, 0, 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方法。

答案 1 :(得分:3)

这只是将数据从b_y1复制到v(160倍)。

最明显的替代品是这样的:

for (int i=0; i<160; i++)
    v[i] = b_y1[i];