值类型结构是基于堆栈的变量,驻留在处理器缓存中而不是 RAM 中。因此,使用值类型变量避免通过系统总线从处理器到 RAM 应该比使用引用类型变量更快。因此,我没有这样做stackallock
(实际使用 `stackalloc` 关键字),而是创建了一个表示基于堆栈的数组的以下结构:
[StructLayout(LayoutKind.Sequential)]
public struct ArrayOf16Bytes
{
public Byte e0;
public Byte e1;
public Byte e2;
public Byte e3;
public Byte e4;
public Byte e5;
public Byte e6;
public Byte e7;
public Byte e8;
public Byte e9;
public Byte eA;
public Byte eB;
public Byte eC;
public Byte eD;
public Byte eE;
public Byte eF;
public byte this[Int32 index] {
get {
switch (index) {
case 0x0:
return e0;
case 0x1:
return e1;
case 0x2:
return e2;
case 0x3:
return e3;
case 0x4:
return e4;
case 0x5:
return e5;
case 0x6:
return e6;
case 0x7:
return e7;
case 0x8:
return e8;
case 0x9:
return e9;
case 0xA:
return eA;
case 0xB:
return eB;
case 0xC:
return eC;
case 0xD:
return eD;
case 0xE:
return eE;
case 0xF:
return eF;
default:
throw new IndexOutOfRangeException ();
}
}
set {
switch (index) {
case 0x0:
e0 = value;
break;
case 0x1:
e1 = value;
break;
case 0x2:
e2 = value;
break;
case 0x3:
e3 = value;
break;
case 0x4:
e4 = value;
break;
case 0x5:
e5 = value;
break;
case 0x6:
e6 = value;
break;
case 0x7:
e7 = value;
break;
case 0x8:
e8 = value;
break;
case 0x9:
e9 = value;
break;
case 0xA:
eA = value;
break;
case 0xB:
eB = value;
break;
case 0xC:
eC = value;
break;
case 0xD:
eD = value;
break;
case 0xE:
eE = value;
break;
case 0xF:
eF = value;
break;
default:
throw new IndexOutOfRangeException ();
}
}
}
}
case
应该编译为jump table
, 因为cmp
和jump
是单周期指令(在 C# 中使用 if/else 和 switch-case 有什么显着区别吗?),第一段代码应该比第二段快得多
在以下示例中,它的工作速度比实际数组慢:
[Test]
public void TestStackArrayPerformance() {
var random = new Xor128 ();
byte[] x = new byte[16];
ArrayOf16Bytes p = new ArrayOf16Bytes ();
for (int i = 0; i < 16; i++) {
x [i] = p [i] = random.As<IUniform<byte>> ().Evaluate ();
}
var index = random.As<IUniform<Int32>> ().Evaluate (0, 15);
var timer = DateTime.Now;
for (int i = 0; i < 1000000000; i++) {
var t = x [i & 0xF];
x [i & 0xF] = x [index];
x [index] = t;
}
Console.WriteLine ("Spinup took: {0}", DateTime.Now - timer);
timer = DateTime.Now;
for (int i = 0; i < 1000000000; i++) {
var t = x [i & 0xF];
x [i & 0xF] = x [index];
x [index] = t;
}
Console.WriteLine ("Operation 1 took: {0}", DateTime.Now - timer);
timer = DateTime.Now;
for (int i = 0; i < 100000000; i++) {
var t = p [i & 0xF];
p [i & 0xF] = p [index];
p [index] = t;
}
Console.WriteLine ("Operation 2 took: {0}", DateTime.Now - timer);
}
在我的机器上,这段代码显示了以下结果:
Spinup took: 00:00:00.3005500
Operation 1 took: 00:00:00.2959800
Operation 2 took: 00:00:04.4344340