C# 将数组向右移动

3

我有一个问题,我已经成功地将内容向左移动,但是我在将其向右移动时遇到了问题。

这是我如何向左移动的示例:

  • 你有一个数组:1、2、3、4、5
  • 向左移动一个:2、3、4、5、1
  • 向右移动一个:5、1、2、3、4

1
可能是 C# 中最快的移动数组方法 的重复问题。 - Rafael Herscovici
4个回答

5

Array.Copybuffer.BlockCopy 在大多数情况下可能是最快的方法。

var temp = ary[ary.Length - 1];
Array.Copy(ary,0, ary,1, ary.Length-1);
ary[0] = temp;

基准测试

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description      : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3401 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

结果

--- Standard input -----------------------------------------------------------
| Value     |    Average |    Fastest |    Cycles | Garbage | Test |    Gain |
--- Scale 1,000 ----------------------------------------------- Time 0.006 ---
| BlockCopy |  39.130 ns |   0.000 ns |   1.354 K | 0.000 B | N/A  | 95.65 % |
| ArrayCopy | 184.615 ns |   0.000 ns |   1.831 K | 0.000 B | N/A  | 79.49 % |
| Unsafe    | 594.828 ns | 300.000 ns |   3.245 K | 0.000 B | N/A  | 33.91 % |
| Index     | 900.000 ns | 900.000 ns |   4.145 K | 0.000 B | Base |  0.00 % |
--- Scale 10,000 ---------------------------------------------- Time 0.002 ---
| BlockCopy | 417.857 ns | 300.000 ns |   2.704 K | 0.000 B | N/A  | 95.26 % |
| ArrayCopy |   1.948 µs |   1.801 µs |   8.065 K | 0.000 B | N/A  | 77.92 % |
| Unsafe    |   6.377 µs |   5.703 µs |  23.116 K | 0.000 B | N/A  | 27.72 % |
| Index     |   8.822 µs |   8.705 µs |  31.689 K | 0.000 B | Base |  0.00 % |
--- Scale 100,000 --------------------------------------------- Time 0.020 ---
| BlockCopy |   4.814 µs |   4.803 µs |  17.882 K | 0.000 B | N/A  | 94.61 % |
| ArrayCopy |  25.016 µs |  24.015 µs |  87.177 K | 0.000 B | N/A  | 71.99 % |
| Unsafe    |  62.844 µs |  58.537 µs | 216.297 K | 0.000 B | N/A  | 29.63 % |
| Index     |  89.307 µs |  82.253 µs | 306.309 K | 0.000 B | Base |  0.00 % |
--- Scale 1,000,000 ------------------------------------------- Time 0.216 ---
| BlockCopy |  63.048 µs |  60.639 µs | 217.764 K | 0.000 B | N/A  | 93.22 % |
| ArrayCopy | 257.222 µs | 234.751 µs | 880.436 K | 0.000 B | N/A  | 72.33 % |
| Unsafe    | 653.073 µs | 604.590 µs |   2.230 M | 0.000 B | N/A  | 29.74 % |
| Index     | 929.520 µs | 843.845 µs |   3.173 M | 0.000 B | Base |  0.00 % |
--- Scale 10,000,000 ------------------------------------------ Time 2.346 ---
| BlockCopy | 998.789 µs | 924.597 µs |   3.414 M | 0.000 B | N/A  | 89.70 % |
| ArrayCopy |   4.875 ms |   4.563 ms |  16.575 M | 0.000 B | N/A  | 49.74 % |
| Unsafe    |   7.225 ms |   6.922 ms |  24.626 M | 0.000 B | N/A  | 25.51 % |
| Index     |   9.699 ms |   9.313 ms |  33.063 M | 0.000 B | Base |  0.00 % |
--- Scale 100,000,000 ---------------------------------------- Time 23.824 ---
| BlockCopy |  11.989 ms |  11.528 ms |  40.759 M | 0.000 B | N/A  | 87.67 % |
| ArrayCopy |  49.423 ms |  46.981 ms | 167.664 M | 0.000 B | N/A  | 49.18 % |
| Unsafe    |  75.024 ms |  71.877 ms | 255.297 M | 0.000 B | N/A  | 22.86 % |
| Index     |  97.254 ms |  95.442 ms | 331.134 M | 0.000 B | Base |  0.00 % |
------------------------------------------------------------------------------

代码

public class Index : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var t = Input[Input.Length - 1];
      for (var i = Input.Length - 1; i > 0; i--)
         Input[i] = Input[i - 1];
      Input[0] = t;
      return Input;
   }
}

public class Unsafe : WorkLoad<int[], int[]>
{
   public override unsafe int[] Run()
   {
      fixed (int* p = Input)
      {
         var t = *(p + (Input.Length - 1));
         for (var i = Input.Length - 1; i > 0; i--)
            *(p + i) = *(p + (i - 1));
         *p = t;
      }
      return Input;
   }
}

public class ArrayCopy : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var temp = Input[Input.Length - 1];
      Array.Copy(Input, 0, Input, 1, Input.Length - 1);
      Input[0] = temp;
      return Input;
   }
}

public class BlockCopy : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var temp = Input[Input.Length - 1];
      Buffer.BlockCopy(Input, 0, Input, 1, Input.Length - 1);
      Input[0] = temp;
      return Input;
   }
}

你用什么工具来对这样的代码进行基准测试?看起来非常有用。 - Javier Capello
@JavierCapello是我为自己在工作和家庭中使用的特定用例编写的自定义工具,但您也可以使用BenchmarkDotNet。 - TheGeneral

3

向右移动与向左移动非常相似,只需保存最后一个元素而不是第一个元素,并从后向前迭代:

int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
    tab[i] = tab[i - 1];
tab[0] = t;

1
之后我的数组看起来像这样:5,1,1,1,1,1 - kubn2
@kubn2 真是太蠢了,我需要同时反转迭代的顺序 - 请看我的编辑答案。 - Mureinik

0

没有测试过,但应该可以工作:

int t = tab[tab.Length-1];

for (int i = 0; i < tab.Length; i++){
    int t2= tab[i];
    tab[i] = t;
    t = t2;
 }

0

最简单的方法:

//right shift
        int[] tmp = new int[tab.Length];
        for (int i = 0; i < tab.Length; i++)
        {
            tmp[(i + 1) % tmp.Length] = tab[i];
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接