c# - 如何在 C# 中快速将二维数组转换为一维数组?

标签 c# arrays

我有一个多维 double[,] 数组,其大小为 [1,N](假设 N 已知)。将其转换为长度为 N 的一维 double[] 数组的最快方法是什么?

我是 C# 的新手,我用它来与 Matlab 函数交互。我使用的 Matlab 函数返回一维行向量。在 C# 中,它被视为 object[,],我只能将其转换为 double[,]。但是,我需要它是 double[] 类型作为另一个函数的输入。有没有一种快速的方法可以将这个二维 double 组转换为具有相同元素和相同顺序的一维数组?

我需要尽可能快地进行转换,因为我正在处理实时应用程序。

最佳答案

最快的方法是直接内存复制方法之一,如Buffer.BlockCopy , 或 Marshal.Copy .

示例

var ary1 = new int[3,3];
ary1[0, 0] = 0;
ary1[0, 1] = 1;
ary1[0, 2] = 2;
ary1[1, 0] = 3;
ary1[1, 1] = 4;
ary1[1, 2] = 5;
ary1[2, 0] = 6;
ary1[2, 1] = 7;
ary1[2, 2] = 8;

var ary2 = new int[9];

Buffer.BlockCopy(ary1, 0, ary2, 0, 9 * sizeof(int));

Console.WriteLine(string.Join(",", ary2));

输出

0,1,2,3,4,5,6,7,8

您也可以使用 memcpy,但是您将承担来自 PInvoke 的初始开销

[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, UIntPtr count);

基准

基准测试运行了 100 次,每次运行后都会收集垃圾,并相互验证准确性,结果是从前 75% 的快速运行中整理出来的,比例是多维数组的维度长度,即 double[比例,比例]

┌──────────────────┬────────────────────────────────────────────┐
│        Test Mode │ Release (64Bit)                            │
│   Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro                   │
│          Version │ 10.0.17763                                 │
├──────────────────┼────────────────────────────────────────────┤
│       CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz    │
│  CPU Description │ Intel64 Family 6 Model 158 Stepping 9      │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│  Cores (Threads) │ 4 (8)    │     Architecture │ x64          │
│      Clock Speed │ 3600 MHz │        Bus Speed │ 100 MHz      │
│          L2Cache │ 1 MB     │          L3Cache │ 8 MB         │
└──────────────────┴──────────┴──────────────────┴──────────────┘

字节数组的结果

┌── Standard input ──────────────────────────────────────────────────────────────────────┐
│ Value           │    Average │    Fastest │    Cycles │ Garbage │ Test │          Gain │
├── Scale 256 ────────────────────────────────────────────────────────────── 0.628 sec ──┤
│ MarshalCopy     │   6.450 µs │   4.300 µs │  26.698 K │ 0.000 B │ Pass │       24.21 % │
│ memcpy          │   7.992 µs │   4.700 µs │  32.758 K │ 0.000 B │ Pass │        6.09 % │
│ BlockCopy       │   8.511 µs │   4.600 µs │  37.053 K │ 0.000 B │ Base │        0.00 % │
│ ElemCopy Unsafe │  26.124 µs │  24.400 µs │  97.794 K │ 0.000 B │ Pass │     -206.96 % │
│ ElemCopy        │  75.426 µs │  72.300 µs │ 273.201 K │ 0.000 B │ Pass │     -786.27 % │
│ Linq            │   7.619 ms │   7.078 ms │  27.103 M │ 0.000 B │ Pass │  -89,429.16 % │
├── Scale 512 ────────────────────────────────────────────────────────────── 1.826 sec ──┤
│ MarshalCopy     │  17.939 µs │  17.300 µs │  68.142 K │ 0.000 B │ Pass │        1.33 % │
│ BlockCopy       │  18.182 µs │  17.300 µs │  69.770 K │ 0.000 B │ Base │        0.00 % │
│ memcpy          │  25.897 µs │  19.200 µs │  97.357 K │ 0.000 B │ Pass │      -42.44 % │
│ ElemCopy Unsafe │ 128.776 µs │ 102.400 µs │ 471.381 K │ 0.000 B │ Pass │     -608.28 % │
│ ElemCopy        │ 293.237 µs │ 285.400 µs │   1.055 M │ 0.000 B │ Pass │   -1,512.82 % │
│ Linq            │  31.057 ms │  29.750 ms │ 110.869 M │ 0.000 B │ Pass │ -170,714.99 % │
├── Scale 1,024 ──────────────────────────────────────────────────────────── 6.579 sec ──┤
│ memcpy          │ 268.747 µs │ 255.600 µs │ 972.409 K │ 0.000 B │ Pass │       12.28 % │
│ BlockCopy       │ 306.371 µs │ 291.500 µs │   1.104 M │ 0.000 B │ Base │        0.00 % │
│ MarshalCopy     │ 307.868 µs │ 293.100 µs │   1.111 M │ 0.000 B │ Pass │       -0.49 % │
│ ElemCopy Unsafe │ 583.684 µs │ 561.100 µs │   2.103 M │ 0.000 B │ Pass │      -90.52 % │
│ ElemCopy        │   1.325 ms │   1.305 ms │   4.768 M │ 0.000 B │ Pass │     -332.50 % │
│ Linq            │ 122.561 ms │ 120.700 ms │ 439.940 M │ 0.000 B │ Pass │  -39,904.01 % │
├── Scale 2,048 ─────────────────────────────────────────────────────────── 26.084 sec ──┤
│ memcpy          │   1.179 ms │   1.129 ms │   4.230 M │ 0.000 B │ Pass │       17.50 % │
│ MarshalCopy     │   1.397 ms │   1.346 ms │   5.029 M │ 0.000 B │ Pass │        2.25 % │
│ BlockCopy       │   1.429 ms │   1.360 ms │   5.135 M │ 0.000 B │ Base │        0.00 % │
│ ElemCopy Unsafe │   2.441 ms │   2.312 ms │   8.757 M │ 0.000 B │ Pass │      -70.88 % │
│ ElemCopy        │   5.466 ms │   5.264 ms │  19.587 M │ 0.000 B │ Pass │     -282.61 % │
│ Linq            │ 497.788 ms │ 489.885 ms │   1.786 B │ 0.000 B │ Pass │  -34,743.98 % │
├── Scale 4,096 ─────────────────────────────────────────────────────────── 42.833 sec ──┤
│ memcpy          │   5.218 ms │   4.889 ms │  18.633 M │ 0.000 B │ Pass │       15.45 % │
│ BlockCopy       │   6.172 ms │   5.887 ms │  22.141 M │ 0.000 B │ Base │        0.00 % │
│ MarshalCopy     │   6.255 ms │   5.871 ms │  22.350 M │ 0.000 B │ Pass │       -1.35 % │
│ ElemCopy Unsafe │   9.972 ms │   9.535 ms │  35.716 M │ 0.000 B │ Pass │      -61.57 % │
│ ElemCopy        │  22.149 ms │  21.741 ms │  79.508 M │ 0.000 B │ Pass │     -258.87 % │
│ Linq            │    1.969 s │    1.948 s │   7.067 B │ 0.000 B │ Pass │  -31,796.88 % │
└────────────────────────────────────────────────────────────────────────────────────────┘

双数组的结果

┌── Standard input ─────────────────────────────────────────────────────────────────────┐
│ Value           │    Average │    Fastest │    Cycles │ Garbage │ Test │         Gain │
├── Scale 256 ───────────────────────────────────────────────────────────── 0.688 sec ──┤
│ BlockCopy       │  35.116 µs │  32.000 µs │ 131.112 K │ 0.000 B │ Base │       0.00 % │
│ MarshalCopy     │  43.879 µs │  34.900 µs │ 162.031 K │ 0.000 B │ Pass │     -24.96 % │
│ ElemCopy Unsafe │  44.182 µs │  39.500 µs │ 162.891 K │ 0.000 B │ Pass │     -25.82 % │
│ memcpy          │  60.113 µs │  58.200 µs │ 219.950 K │ 0.000 B │ Pass │     -71.19 % │
│ ElemCopy        │  94.418 µs │  86.100 µs │ 356.173 K │ 0.000 B │ Pass │    -168.88 % │
│ Linq            │   7.402 ms │   7.123 ms │  26.638 M │ 0.000 B │ Pass │ -20,979.54 % │
├── Scale 512 ───────────────────────────────────────────────────────────── 2.237 sec ──┤
│ memcpy          │ 612.603 µs │ 552.000 µs │   2.199 M │ 0.000 B │ Pass │      10.20 % │
│ ElemCopy Unsafe │ 641.013 µs │ 586.200 µs │   2.312 M │ 0.000 B │ Pass │       6.03 % │
│ MarshalCopy     │ 675.192 µs │ 621.200 µs │   2.434 M │ 0.000 B │ Pass │       1.02 % │
│ BlockCopy       │ 682.161 µs │ 622.700 µs │   2.458 M │ 0.000 B │ Base │       0.00 % │
│ ElemCopy        │ 745.692 µs │ 708.800 µs │   2.687 M │ 0.000 B │ Pass │      -9.31 % │
│ Linq            │  33.579 ms │  31.039 ms │ 119.974 M │ 0.000 B │ Pass │  -4,822.46 % │
├── Scale 1,024 ─────────────────────────────────────────────────────────── 7.830 sec ──┤
│ memcpy          │   2.708 ms │   2.488 ms │   9.712 M │ 0.000 B │ Pass │      20.63 % │
│ ElemCopy Unsafe │   3.156 ms │   2.789 ms │  11.324 M │ 0.000 B │ Pass │       7.51 % │
│ MarshalCopy     │   3.208 ms │   2.979 ms │  11.508 M │ 0.000 B │ Pass │       5.97 % │
│ ElemCopy        │   3.342 ms │   3.091 ms │  12.021 M │ 0.000 B │ Pass │       2.05 % │
│ BlockCopy       │   3.412 ms │   2.959 ms │  12.234 M │ 0.000 B │ Base │       0.00 % │
│ Linq            │ 125.854 ms │ 122.872 ms │ 451.735 M │ 0.000 B │ Pass │  -3,588.76 % │
├── Scale 2,048 ────────────────────────────────────────────────────────── 29.876 sec ──┤
│ memcpy          │  10.989 ms │  10.288 ms │  39.509 M │ 0.000 B │ Pass │      15.14 % │
│ ElemCopy Unsafe │  12.075 ms │  11.418 ms │  43.436 M │ 0.000 B │ Pass │       6.76 % │
│ BlockCopy       │  12.950 ms │  12.462 ms │  46.578 M │ 0.000 B │ Base │       0.00 % │
│ MarshalCopy     │  13.032 ms │  12.427 ms │  46.876 M │ 0.000 B │ Pass │      -0.64 % │
│ ElemCopy        │  13.469 ms │  12.689 ms │  48.471 M │ 0.000 B │ Pass │      -4.01 % │
│ Linq            │ 502.897 ms │ 497.335 ms │   1.805 B │ 0.000 B │ Pass │  -3,783.35 % │
├── Scale 4,096 ────────────────────────────────────────────────────────── 58.669 sec ──┤
│ memcpy          │  45.901 ms │  44.148 ms │ 164.750 M │ 0.000 B │ Pass │      15.80 % │
│ ElemCopy Unsafe │  51.889 ms │  50.497 ms │ 186.137 M │ 0.000 B │ Pass │       4.82 % │
│ MarshalCopy     │  53.237 ms │  51.847 ms │ 191.248 M │ 0.000 B │ Pass │       2.34 % │
│ BlockCopy       │  54.514 ms │  52.417 ms │ 195.778 M │ 0.000 B │ Base │       0.00 % │
│ ElemCopy        │  56.551 ms │  54.674 ms │ 203.163 M │ 0.000 B │ Pass │      -3.74 % │
│ Linq            │    2.004 s │    1.976 s │   7.192 B │ 0.000 B │ Pass │  -3,575.84 % │
└───────────────────────────────────────────────────────────────────────────────────────┘

测试代码

[Test("BlockCopy", "", true)]
public double[] Test1(double[,] input, int scale)
{
   var width = input.GetLength(0);
   var height = input.GetLength(1);
   var size = width * height;
   var result = new double[size];
   Buffer.BlockCopy(input, 0, result, 0, size * sizeof(double));
   return result;
}

[Test("MarshalCopy", "", false)]
public unsafe double[] Test2(double[,] input, int scale)
{
   var width = input.GetLength(0);
   var height = input.GetLength(1);
   var size = width * height;
   var result = new double[size];
   fixed (double* pInput = input)
      Marshal.Copy((IntPtr)pInput, result, 0, size );
   return result;
}

[Test("ElemCopy", "", false)]
public double[] Test3(double[,] input, int scale)
{
   var width = input.GetLength(0);
   var height = input.GetLength(1);
   var size = width * height;

   var result = new double[size];
   for (var i = 0; i < width; i++)
      for (var j = 0; j < height; j++)
         result[i * height + j] = input[i,j];
   return result;
}
[Test("ElemCopy Unsafe", "", false)]
unsafe public double[] Test4(double[,] input, int scale)
{
   var width = input.GetLength(0);
   var height = input.GetLength(1);
   var size = width * height;

   var result = new double[size];
   fixed (double* pInput = input, pResult = result)
      for (var i = 0; i < width; i++)
         for (var j = 0; j < height; j++)
            *(pResult + i * height + j) = *(pInput + i * height + j);
   return result;
}
[Test("memcpy", "", false)]
unsafe public double[] Test5(double[,] input, int scale)
{
   var width = input.GetLength(0);
   var height = input.GetLength(1);
   var size = width * height;

   var result = new double[size];
   fixed (double* pInput = input, pResult = result)
      memcpy((IntPtr)pResult, (IntPtr)pInput, (UIntPtr)(size * sizeof(double)));
   return result;
}
[Test("Linq", "", false)]
unsafe public double[] Test6(double[,] input, int scale)
{
   return input.OfType<double>().ToArray();
}

注意:您可能应该在自己的规范 pc、框架等上自己运行这些测试,并且应该仅用作指南

关于c# - 如何在 C# 中快速将二维数组转换为一维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55527580/

相关文章:

PHP/在查询中插入 foreach

Javascript从存储在索引上的数组内的对象获取键

java - Java中如何持续分配内存直到分配完3GB?

c# - 为 64 位编译我的应用程序如何使其更快或更好?

c# - EntityFramework where 子句是否始终按相同顺序返回对象

c# - 转换分割字符串时出错

php - PHP 函数 in_array(...) 有什么问题?

c++ - 尝试对数组进行排序

c# - 我应该如何依赖 Session

c# - LINQ to SQL 其中 ID 不在 some_list 中