您好,我有以下片段

  for(int k = 0; k< 1000; ++k)
    {

        double acc = 1.0;

        if(...)
        {
            short amSeq = 100;

            for (short c = 0; c <= 21; ++c)
            {
                for (short kk = (Range.uSequences[k][c]), s = SeqComp[c]; kk != 0; --kk, --s, --amSeq)
                {
                    acc *= static_cast<double>(s) / amSeq;
                }
            }
        }
        else
            acc = 0;

    }


我想知道是否有任何方法可以优化acc *= static_cast<double>(s) / amSeq;行,删除static_cast可使速度提高200倍,但显然会产生不正确的结果。谢谢

最佳答案

您可以通过移动代码并使用正确的类型来轻松避免转换:

if(...)
{
    double amSeq = 100;

    for (short c = 0; c <= 21; ++c)
    {
        double s = SeqComp[c];

        for (short kk = (Range.uSequences[k][c]); kk != 0; --kk, --s, --amSeq)
        {
                acc *= s / amSeq;
        }
    }
}

09-19 21:35