首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化钻头的重排

问优化钻头的重排
EN

Stack Overflow用户
提问于 2015-11-18 21:30:21
回答 1查看 196关注 0票数 1

我有一个核心的C#函数,我正试图加速它。涉及安全或不安全代码的建议同样受到欢迎。以下是一种方法:

代码语言:javascript
复制
public byte[] Interleave(uint[] vector)
{
    var byteVector = new byte[BytesNeeded + 1]; // Extra byte needed when creating a BigInteger, for sign bit.
    foreach (var idx in PrecomputedIndices)
    {
        var bit = (byte)(((vector[idx.iFromUintVector] >> idx.iFromUintBit) & 1U) << idx.iToByteBit);
        byteVector[idx.iToByteVector] |= bit;
    }
    return byteVector;
}

PrecomputedIndices是以下类的数组:

代码语言:javascript
复制
class Indices
{
    public readonly int iFromUintVector;
    public readonly int iFromUintBit;
    public readonly int iToByteVector;
    public readonly int iToByteBit;

    public Indices(int fromUintVector, int fromUintBit, int toByteVector, int toByteBit)
    {
        iFromUintVector = fromUintVector;
        iFromUintBit = fromUintBit;
        iToByteVector = toByteVector;
        iToByteBit = toByteBit;
    }
}

Interleave方法的目的是将字节数组中的位复制到字节数组中。我已经预先计算了源和目标数组索引以及源和目标位数,并将它们存储在索引对象中。源中没有两个相邻位在目标中相邻,因此排除了某些优化。

为了给你一个关于尺度的概念,我正在研究的问题大约有4200维,所以“向量”有4200个元素。向量中的值从零到十二,所以我只需要使用四位来将它们的值存储在字节数组中,因此我需要4,200 x 4= 16,800位数据,或者每个向量输出2,100字节。这种方法将被称为数百万次。在我需要优化的更大的过程中,它消耗了大约三分之一的时间。

更新1:将“索引”更改为一个结构,并缩小一些数据类型,使对象仅为8个字节(一个int,一个短字节和两个字节),使执行时间的百分比从35%减少到30%。

EN

回答 1

Stack Overflow用户

发布于 2015-11-19 19:02:21

以下是我修订后的执行工作的关键部分,并从评论者那里得出了一些想法:

  1. 将对象转换为struct,将数据类型收缩为较小的int,然后重新排列,使对象适应64位值,这对于64位计算机来说更好: 结构索引{/索引为源向量读取源uint。/ public readonly;/索引到目标字节的目标向量中进行写入。/ public只读短iToByteVector;/索引为源uint的源位来读取。/公共只读字节iFromUintBit;/索引为目标位的目标字节进行写入。/ public只读字节iToByteBit;公共索引(int fromUintVector,字节fromUintBit,短toByteVector,字节toByteBit) { iFromUintVector = fromUintVector;iFromUintBit = fromUintBit;iToByteVector = toByteVector;iToByteBit = toByteBit;}
  2. 对PrecomputedIndices进行排序,以便按升序写入每个目标字节和位,从而改善内存缓存访问: 比较sortByTargetByteAndBit = (a,b) => { if (a.iToByteVector < b.iToByteVector)返回-1;if (a.iToByteVector > b.iToByteVector)返回1;if (a.iToByteBit < b.iToByteBit)返回-1;if (a.iToByteBit > b.iToByteBit)返回1;返回0;};Array.Sort(PrecomputedIndices,sortByTargetByteAndBit);
  3. 展开循环,以便同时组装整个目标字节,减少我访问目标数组的次数: 公共byte[]交织(uint[]向量){ var byteVector =新的byteBytesNeeded + 1;//需要额外的字节来保存额外的位和BigInteger的符号位。var extraBits = BytesNeeded << 3;int iIndex = 0;var iByte = 0;for (;iByte < BytesNeeded;iByte++) { //展开循环,因此我们一次计算整个字节的比特。uint位= 0;var idx0 = PrecomputedIndicesiIndex;var idx1 = PrecomputedIndicesiIndex + 1;var idx2 = PrecomputedIndicesiIndex + 2;var idx3 = PrecomputedIndicesiIndex + 3;var idx4 = PrecomputedIndicesiIndex + 4;var idx5 = PrecomputedIndicesiIndex + 5;var idx6 = PrecomputedIndicesiIndex + 6;var idx7 = PrecomputedIndicesiIndex + 7;bits =(vectoridx0.iFromUintVectoridx0.iFromUintBit)& 1U) (vectoridx1.iFromUintVectoridx1.iFromUintBit)& 1U) >> 1) (vectoridx2.iFromUintVectoridx2.iFromUintBit)& 1U) << 2) (Vetortoridx3.iFromUintVector>> idx3.iFromUintBit) &1U) << 3) (vitortoridx4.iFumrom>> idx4.iFromUintBit) )(向量)6.iFromUintBit)& 1U) << 5) (Vetoridx6.iFromUintBit)& 1U) << 6) (向量)7.iFromUintVector>>idx7.iFromBit)& 1U) << 7;byteVectoriByte =(字节)位;iIndex += 8;} for (;iIndex < PrecomputedIndices.Length;iIndex++) { var idx = PrecomputedIndicesiIndex;var位=(字节)(vectoridx.iFromUintVector >> idx.iFromUintBit) & 1U) << idx.iToByteBit);byteVectoridx.iToByteVector |=位;}返回byteVector;}
  • #1将功能从占执行时间的35%减少到占执行时间的30% (节省14%)。
  • #2没有加快这个函数的速度,但是使#3成为可能。
  • #3将功能从30%的执行时间削减到19.6%,再节省33%。

总储蓄:44%!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33790773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档