对于循环减慢 [英] For loop slows down

查看:182
本文介绍了对于循环减慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个循环遍历文档向量(具体类型,由m_docs指向)的程序。每个文档都有一个属性,它是在某些场合(循环的点)改变的〜17000个零的向量。我有〜3200的文档。我的问题是,第一百个文档的处理相当快,然后真的放慢。

I'm writing a program that loops through a vector of documents (specific type, pointed by m_docs). Each doc has an attribute which is a vector of ~17000 zeros that are changed on some occasions (the point of the loop). I have ~3200 docs. My problem is that the first hundred docs are processed rather quickly, and then it really slows down. I would like to understand why it slows down, and to know how I could fix it (or at least optimize it)

有问题的部分代码:

for (int k = 0; k < m_docs->size(); k++) {
    int pos;
    std::map<std::string, std::vector<std::pair<int, int> > >::iterator it = m_index.begin();
    std::map<string,int> cleanList = (*m_docs)[k].getCleantList();
    for (auto const& p : cleanList) {
        pos = distance(it, m_index.find(p.first));
        float weight = computeIdf(p.first) * computeTf(p.first, (*m_docs)[k]);
        (*m_docs)[k].setCoord(pos, weight);
    }
}


推荐答案


  1. 这可能更有效:

  1. This could be more efficient:

std::map<string,int> cleanList

into

std::map<string,int> const& cleanList

最差的情况下, getCleantList 复制,并且你得到一个临时绑定到const& (这是罚款)。

Worst case, getCleantList already made the copy, and you get a temp bound to a const& (which is fine). But way more likely, you decimate memory allocations because you're no longer copying maps containing strings

另外,在这里查看搜索的效率:

Also, look at the efficiency of the search here:

pos = distance(it, m_index.find(p.first));

调用变量 m_index 。您可能需要改进位置(flat_map)或使用基于散列的容器(例如unordered_map)

You called the variable m_index. You might need to improve locality (flat_map) or use a hash based container (unordered_map e.g.)

查看数据结构(至少对于 m_index

Review your data structures (at the very least for the m_index)

这篇关于对于循环减慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆