vector.push_back()提高性能

发布于 2025-02-13 11:24:47 字数 1023 浏览 0 评论 0原文

有什么方法可以改善此功能的性能?正如我必须称其为〜10-100m次以进行1个计算,它正在放慢我的程序,并且大多数时间都在这条线路中丢失了:

myHand_param.FinalHandVector.push_back((1 << static_cast<int>(std::ceil((i - 3.0) / 4.0))));

我知道矢量容量绝不应该超过5,但我不知道如何跳过边界检查,试图与int*一起工作*而不是向量,但也没有帮助。

    void CalculateKickers(Hand& myHand_param, int kickersNeeded)
{
    int countKickers{ 0 };

    for(int i = bits-1; i > 0 ; i--)
    {
        if (static_cast<std::bitset<bits>>(myHand_param.handMask)[i] == 1)
        {
            if (static_cast<std::bitset<1>>((static_cast<std::bitset<bits>>(myHand_param.FinalHandUsedCardFacesMask) >> i).to_ullong())== 0)
            {
                ++countKickers;
                myHand_param.FinalHandVector.push_back((1 << static_cast<int>(std::ceil((i - 3.0) / 4.0))));

                if (countKickers == kickersNeeded)
                {
                    i = 0;
                }
            }
        }   
    }
}

Is there any way to improve the performance of this function? As I have to call it ~10-100M times for 1 calculation it's slowing down my program and most time has been lost in this line:

myHand_param.FinalHandVector.push_back((1 << static_cast<int>(std::ceil((i - 3.0) / 4.0))));

I know that the vector capacity should never exceed 5 but I don't know how to skip the boundary checking, tried to work with int* instead of vector but it didn't help as well.

    void CalculateKickers(Hand& myHand_param, int kickersNeeded)
{
    int countKickers{ 0 };

    for(int i = bits-1; i > 0 ; i--)
    {
        if (static_cast<std::bitset<bits>>(myHand_param.handMask)[i] == 1)
        {
            if (static_cast<std::bitset<1>>((static_cast<std::bitset<bits>>(myHand_param.FinalHandUsedCardFacesMask) >> i).to_ullong())== 0)
            {
                ++countKickers;
                myHand_param.FinalHandVector.push_back((1 << static_cast<int>(std::ceil((i - 3.0) / 4.0))));

                if (countKickers == kickersNeeded)
                {
                    i = 0;
                }
            }
        }   
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文