C#将数组向右移动
所以我有一个问题,我成功地向左移动了,但我在向右移动时遇到了问题 这就是我向左移动的方式: 示例我的意思是向左和向右移动:C#将数组向右移动,c#,arrays,C#,Arrays,所以我有一个问题,我成功地向左移动了,但我在向右移动时遇到了问题 这就是我向左移动的方式: 示例我的意思是向左和向右移动: 你有一个数组:1,2,3,4,5 向左移动一个:2,3,4,5,1 右移一位:5,1,2,3,4 向右移动非常相似,因此向左移动时,只需保存最后一个元素,而不是第一个元素,然后从末尾向后迭代: int t = tab[tab.Length - 1]; for (int i = tab.Length - 1; i > 0; i--) tab[i] = tab[
- 你有一个数组:1,2,3,4,5
- 向左移动一个:2,3,4,5,1
- 右移一位:5,1,2,3,4
int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
tab[i] = tab[i - 1];
tab[0] = t;
向右移动非常相似,因此向左移动时,只需保存最后一个元素,而不是第一个元素,然后从末尾向后迭代:
int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
tab[i] = tab[i - 1];
tab[0] = t;
没有测试它,但它应该可以工作:
int t = tab[tab.Length-1];
for (int i = 0; i < tab.Length; i++){
int t2= tab[i];
tab[i] = t;
t = t2;
}
int t=tab[tab.Length-1];
for(int i=0;i
没有测试它,但它应该可以工作:
int t = tab[tab.Length-1];
for (int i = 0; i < tab.Length; i++){
int t2= tab[i];
tab[i] = t;
t = t2;
}
int t=tab[tab.Length-1];
for(int i=0;i
Array.Copy
和buffer.BlockCopy
可能是大多数情况下最快的方法
var temp = ary[ary.Length - 1];
Array.Copy(ary,0, ary,1, ary.Length-1);
ary[0] = temp;
基准
结果
--- 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;
}
}
公共类索引:工作负载
{
公共覆盖int[]运行()
{
var t=输入[Input.Length-1];
对于(var i=Input.Length-1;i>0;i--)
输入[i]=输入[i-1];
输入[0]=t;
返回输入;
}
}
公共类:工作负载
{
公共覆盖不安全的int[]运行()
{
固定(int*p=输入)
{
var t=*(p+(Input.Length-1));
对于(var i=Input.Length-1;i>0;i--)
*(p+i)=*(p+(i-1));
*p=t;
}
返回输入;
}
}
公共类ArrayCopy:工作负载
{
公共覆盖int[]运行()
{
var temp=输入[Input.Length-1];
复制(输入,0,输入,1,输入。长度-1);
输入[0]=温度;
返回输入;
}
}
公共类区块复制:工作负载
{
公共覆盖int[]运行()
{
var temp=输入[Input.Length-1];
块复制(输入,0,输入,1,输入。长度-1);
输入[0]=温度;
返回输入;
}
}
Array.Copy
和buffer.BlockCopy
可能是大多数情况下最快的方法
var temp = ary[ary.Length - 1];
Array.Copy(ary,0, ary,1, ary.Length-1);
ary[0] = temp;
基准
结果
--- 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;
}
}
公共类索引:工作负载
{
公共覆盖int[]运行()
{
var t=输入[Input.Length-1];
对于(var i=Input.Length-1;i>0;i--)
输入[i]=输入[i-1];
输入[0]=t;
返回输入;
}
}
公共类:工作负载
{
公共覆盖不安全的int[]运行()
{
固定(int*p=输入)
{
var t=*(p+(Input.Length-1));
对于(var i=Input.Length-1;i>0;i--)
*(p+i)=*(p+(i-1));
*p=t;
}
返回输入;
}
}
公共类ArrayCopy:工作负载
{
公共覆盖int[]运行()
{
var temp=输入[Input.Length-1];
复制(输入,0,输入,1,输入。长度-1);
输入[0]=温度;
返回输入;
}
}
公共类区块复制:工作负载
{
公共覆盖int[]运行()
{
var temp=输入[Input.Length-1];
块复制(输入,0,输入,1,输入。长度-1);
输入[0]=温度;
返回输入;
}
}
最简单的方法:
//right shift
int[] tmp = new int[tab.Length];
for (int i = 0; i < tab.Length; i++)
{
tmp[(i + 1) % tmp.Length] = tab[i];
}
//右移
int[]tmp=新的int[tab.Length];
for(int i=0;i
最简单的方法:
//right shift
int[] tmp = new int[tab.Length];
for (int i = 0; i < tab.Length; i++)
{
tmp[(i + 1) % tmp.Length] = tab[i];
}
//右移
int[]tmp=新的int[tab.Length];
for(int i=0;i
可能重复的可能重复之后,我的数组看起来是这样的:5,1,1,1,1,@kubn2该死,我真傻。你还需要颠倒迭代的顺序-看看我编辑过的答案。之后我的数组看起来是这样的:5,1,1,1,1,@kubn2该死,我真傻。您还需要颠倒迭代的顺序-请参阅我编辑的答案。您使用什么工具对这样的代码进行基准测试?它看起来很漂亮useful@JavierCapello是我为我在工作和家庭中使用的特定用例编写的自定义工具,但是您可以使用BenchmarkDotNetW您使用什么工具来对这样的代码进行基准测试?它看起来很漂亮useful@JavierCapello是我为工作和家庭中使用的特定用例编写的自定义工具,但是您可以使用benchmarkDotNet