gpt4 book ai didi

C# 右移数组

转载 作者:行者123 更新时间:2023-12-01 22:04:50 25 4
gpt4 key购买 nike

所以我有一个问题,我确实成功地向左移动了,但我在向右移动时遇到了问题这就是我向左移动的方式:

例如我的意思是向左和向右移动:

  • 你有一个数组:1, 2, 3, 4, 5,
  • 左移一位:2, 3, 4, 5, 1,
  • 右移一位:5, 1, 2, 3, 4,

最佳答案

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;
}
}

关于C# 右移数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com