C ++运行时效率

时间:2014-02-18 03:21:01

标签: c++ runtime

我正在用节点创建一个六角形板。我在这段代码中所拥有的一切都能很好地工作。我的问题是加星标的项目从这个循环减慢了大约1秒到大约1分钟。它不是向量中的项目数,因为如果除了最后一个项目之外我将它们全部删除它仍然需要约1分钟。无论哪种方式都没有运行时错误。

for (x = 0; x < size; ++x)
{
    for (y = 0; y < size; ++y)
    {
        this->nodes[x][y].neighbors = std::vector<node>(6);
        if ((x == 0) && (y == 0))
        {
            this->nodes[x][y].neighbors =
            {
                nodes[x + 1][y],
                this->nodes[x][y + 1]
            };
        }
        else if ((x == 0) && (y == max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x + 1][y],
                this->nodes[x][y - 1],
                this->nodes[x + 1][y - 1]
            };
        }
        else if ((x == max) && (y == 0))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x - 1][y],
                this->nodes[x][y + 1],
                this->nodes[x - 1][y + 1]
            };
        }
        else if ((x == max) && (y == max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x - 1][y],
                this->nodes[x][y - 1]
            };
        }
        else if (y == 0 && (x != 0 && x != max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x - 1][y],
                this->nodes[x + 1][y],
                this->nodes[x - 1][y + 1],
                this->nodes[x][y + 1]
            };
        }
        else if (y == max && (x != 0 && x != max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x - 1][y],
                this->nodes[x + 1][y],
                this->nodes[x - 1][y - 1],
                this->nodes[x][y - 1]
            };
        }
        else if (x == 0 && (y != 0 && y != max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x][y - 1],
                this->nodes[x][y + 1],
                this->nodes[x + 1][y],
                this->nodes[x + 1][y - 1]
            };
        }
        else if (x == max && (y != 0 && y != max))
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x][y - 1],
                this->nodes[x][y + 1],
                this->nodes[x - 1][y - 1],
                this->nodes[x - 1][y]
            };
        }
        else
        {
            this->nodes[x][y].neighbors =
            {
                this->nodes[x + 1][y],
                this->nodes[x - 1][y],
                this->nodes[x][y - 1],
                this->nodes[x + 1][y - 1],
                this->nodes[x][y + 1],
                this->nodes[x - 1][y + 1]
            };
        }
    }
}

2 个答案:

答案 0 :(得分:0)

缓慢来自两件事:

  • 最后一个else子句受到的影响最大。
  • 最后一个子句复制的节点项多于其余的

一些建议:

  • 为邻居使用指针或索引而非node个对象。减少复制。
  • 为邻居提供固定大小的数组会更快。将显着改善局部性和记忆性。您必须为该阵列添加邻居计数。

答案 1 :(得分:0)

鉴于这个想法是跟踪你知道已经预先创建的邻居,你可能只想保留一个引用或指针;您不应该像现在一样 按价值复制node

您可以使用vector<reference_wrapper<node>>vector<node*>进行相应的初始化,如下所示:

nodes[x][y].neighbors = { ref(nodes[a][b]), ref(nodes[c][d])... };

nodes[x][y].neighbors = { &nodes[a][b], &nodes[c][d], ... };

使用指针,当您访问neighbors[n]时,您显然也需要取消引用指针。

关于它为什么这么慢......按值复制意味着将node复制到neighbours是复制所有邻近的node邻居及其邻居,以及恶心...随着长邻居层次结构的节点数量的增加,速度越来越慢。