0

您好,我有以下代码段

  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 倍,但显然会产生不正确的结果。谢谢

4

1 回答 1

2

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

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;
        }
    }
}
于 2012-04-18T19:28:24.963 回答