简体   繁体   English

在std :: list和std :: vector之间选择

[英]Choosing between std::list and std::vector

I have wrote a code which tries to find the repetitions in a vector. 我写了一个代码,试图找到向量中的重复。 In case of a repetition, it will add the position to a list. 如果重复,它将把位置添加到列表中。 For example a sequence of 100 110 90 100 140 90 100 will be a 2D vector. 例如, 100 110 90 100 140 90 100的序列将是2D向量。 The first dimension contains unique letters (or numbers) and a list of repetitions are appended as a second dimension. 第一维包含唯一字母(或数字),重复列表作为第二维附加。 So the result looks like 所以结果看起来像

100 -> [0 3 6]
110 -> [1]
90 -> [2 5]
140 -> [4]

The code is fairly simple 代码很简单

typedef unsigned long long int ulong;
typedef std::vector<ulong> aVector;
struct entry {
  entry( ulong a, ulong t ) {
    addr = a;
    time.push_back(t);
  }
  ulong addr;
  aVector time;
};

// vec contains original data
// theVec is the output vector
void compress( aVector &vec, std::vector< entry > &theVec )
{
   aVector::iterator it = vec.begin();
   aVector::iterator it_end = vec.end();
   std::vector< entry >::iterator ait;
   for (ulong i = 0; it != it_end; ++it, ++i) {  // iterate over input vector
     ulong addr = *it;
     if (theVec.empty()) {  // insert the first item
       theVec.push_back( entry(addr, i) );
       continue;
     }
     ait = find_if( theVec.begin(), theVec.end(), equal_addr(addr));
     if (ait == theVec.end()) { // entry doesn't exist in compressed vector, so insert
       theVec.push_back( entry(addr, i) );
     } else { // write down the position of the repeated number (second dimension)
       ait->time.push_back(i);
     }
   }
}

The find_if will look up like this find_if将看起来像这样

struct equal_addr : std::unary_function<entry,bool>
{
  equal_addr(const ulong &anAddr) : theAddr(anAddr) {}
  bool operator()(const entry &arg) const { return arg.addr == theAddr; }
  const ulong &theAddr;
};

Problem is, for moderate input sizes (20M for my tests), the code is very slow and it may take one day to exit the compress function. 问题是,对于中等大小的输入(在我的测试中为20M),代码非常慢,并且可能需要一天的时间才能退出compress函数。 Is there any chance for speedup by using std::list instead of std::vec ? 通过使用std::list而不是std::vec可以加速吗? Since list is performs better for sequential things. 因为list对于顺序的事情表现更好。 However I just want to know, if it can help or not. 但是,我只想知道它是否可以提供帮助。 If it is useful then I have change some other codes. 如果有用,那么我需要更改一些其他代码。

Looking for any advice. 寻找任何建议。

  1. why don't you try it, and measure the results for yourself? 您为什么不尝试一下,自己衡量结果呢?
  2. no, list does not perform better for "sequential things". 不, list对于“顺序事物”的执行效果不佳。 It performs dramatically worse for everything . 它对所有事情的表现都非常差。

The only real advantage it has is that elements in a list are stable and pointers/iterators to elements won't be eliminated as the list is modified or grown. 它唯一的真正优点是list中的元素是稳定的,并且在列表被修改或增长时,不会消除指向元素的指针/迭代器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM