<i id='eghSE'><tr id='eghSE'><dt id='eghSE'><q id='eghSE'><span id='eghSE'><b id='eghSE'><form id='eghSE'><ins id='eghSE'></ins><ul id='eghSE'></ul><sub id='eghSE'></sub></form><legend id='eghSE'></legend><bdo id='eghSE'><pre id='eghSE'><center id='eghSE'></center></pre></bdo></b><th id='eghSE'></th></span></q></dt></tr></i><div id='eghSE'><tfoot id='eghSE'></tfoot><dl id='eghSE'><fieldset id='eghSE'></fieldset></dl></div>
<legend id='eghSE'><style id='eghSE'><dir id='eghSE'><q id='eghSE'></q></dir></style></legend>

    <tfoot id='eghSE'></tfoot>

    <small id='eghSE'></small><noframes id='eghSE'>

      • <bdo id='eghSE'></bdo><ul id='eghSE'></ul>

        随机插入/删除的综合向量与链表基准

        Comprehensive vector vs linked list benchmark for randomized insertions/deletions(随机插入/删除的综合向量与链表基准)
      1. <tfoot id='RD0yV'></tfoot>

              <tbody id='RD0yV'></tbody>
              <bdo id='RD0yV'></bdo><ul id='RD0yV'></ul>
            • <small id='RD0yV'></small><noframes id='RD0yV'>

              • <i id='RD0yV'><tr id='RD0yV'><dt id='RD0yV'><q id='RD0yV'><span id='RD0yV'><b id='RD0yV'><form id='RD0yV'><ins id='RD0yV'></ins><ul id='RD0yV'></ul><sub id='RD0yV'></sub></form><legend id='RD0yV'></legend><bdo id='RD0yV'><pre id='RD0yV'><center id='RD0yV'></center></pre></bdo></b><th id='RD0yV'></th></span></q></dt></tr></i><div id='RD0yV'><tfoot id='RD0yV'></tfoot><dl id='RD0yV'><fieldset id='RD0yV'></fieldset></dl></div>

                • <legend id='RD0yV'><style id='RD0yV'><dir id='RD0yV'><q id='RD0yV'></q></dir></style></legend>
                  本文介绍了随机插入/删除的综合向量与链表基准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我知道 this 问题,以及其他关于 SO 的问题问题,但其中大部分处理数据结构的复杂性(只是复制到这里,理论上链接这个有 O(

                  So I am aware of this question, and others on SO that deal with issue, but most of those deal with the complexities of the data structures (just to copy here, linked this theoretically has O(

                  我理解复杂性似乎表明列表会更好,但我更关心现实世界的表现.

                  I understand the complexities would seem to indicate that a list would be better, but I am more concerned with the real world performance.

                  注意:这个问题的灵感来自 slides45 和 46 Bjarne Stroustrup 在 Going Native 2012 上的演讲,在那里他谈到了处理器缓存和引用的局部性如何真正帮助向量,但完全(或足够)帮助列表.

                  Note: This question was inspired by slides 45 and 46 of Bjarne Stroustrup's presentation at Going Native 2012 where he talks about how processor caching and locality of reference really help with vectors, but not at all (or enough) with lists.

                  问题: 是否有一种使用 CPU 时间而不是挂起时间来测试这一点的好方法,并获得一种随机"插入和删除可以事先完成的元素的体面方法不影响时间?

                  Question: Is there a good way to test this using CPU time as opposed to wall time, and getting a decent way of "randomly" inserting and deleting elements that can be done beforehand so it does not influence the timings?

                  作为奖励,如果能够将其应用于两个任意数据结构(例如向量和哈希映射或类似的东西)以在某些硬件上找到真实世界的性能",那就太好了.

                  As a bonus, it would be nice to be able to apply this to two arbitrary data structures (say vector and hash maps or something like that) to find the "real world performance" on some hardware.

                  推荐答案

                  我想如果我要测试这样的东西,我可能会从这个顺序的代码开始:

                  I guess if I were going to test something like this, I'd probably start with code something on this order:

                  #include <list>
                  #include <vector>
                  #include <algorithm>
                  #include <deque>
                  #include <time.h>
                  #include <iostream>
                  #include <iterator>
                  
                  static const int size = 30000;
                  
                  template <class T>
                  double insert(T &container) {
                      srand(1234);
                      clock_t start = clock();
                      for (int i=0; i<size; ++i) {
                          int value = rand();
                          T::iterator pos = std::lower_bound(container.begin(), container.end(), value);
                          container.insert(pos, value);
                      }
                  // uncomment the following to verify correct insertion (in a small container).
                  //  std::copy(container.begin(), container.end(), std::ostream_iterator<int>(std::cout, "	"));
                      return double(clock()-start)/CLOCKS_PER_SEC;
                  }
                  
                  
                  template <class T>
                  double del(T &container) {
                      srand(1234);
                      clock_t start = clock();
                      for (int i=0; i<size/2; ++i) {
                          int value = rand();
                          T::iterator pos = std::lower_bound(container.begin(), container.end(), value);
                          container.erase(pos);
                      }
                      return double(clock()-start)/CLOCKS_PER_SEC;
                  }       
                  
                  int main() { 
                      std::list<int> l;
                      std::vector<int> v;
                      std::deque<int> d;
                  
                      std::cout << "Insertion time for list: " << insert(l) << "
                  ";
                      std::cout << "Insertion time for vector: " << insert(v) << "
                  ";
                      std::cout << "Insertion time for deque: " << insert(d) << "
                  
                  ";
                  
                      std::cout << "Deletion time for list: " << del(l) << '
                  ';
                      std::cout << "Deletion time for vector: " << del(v) << '
                  ';
                      std::cout << "Deletion time for deque: " << del(d) << '
                  ';
                  
                      return 0;
                  }
                  

                  因为它使用clock,这应该给处理器时间而不是挂墙时间(尽管一些编译器,如MS VC++ 弄错了).它不会尝试测量插入时间,不包括找到插入点的时间,因为 1) 这需要更多的工作 2) 我仍然无法弄清楚它会完成什么.它当然不是 100% 严格,但考虑到我从中看到的差异,如果看到更仔细的测试有显着差异,我会有点惊讶.例如,使用 MS VC++,我得到:

                  Since it uses clock, this should give processor time not wall time (though some compilers such as MS VC++ get that wrong). It doesn't try to measure the time for insertion exclusive of time to find the insertion point, since 1) that would take a bit more work and 2) I still can't figure out what it would accomplish. It's certainly not 100% rigorous, but given the disparity I see from it, I'd be a bit surprised to see a significant difference from more careful testing. For example, with MS VC++, I get:

                  Insertion time for list: 6.598
                  Insertion time for vector: 1.377
                  Insertion time for deque: 1.484
                  
                  Deletion time for list: 6.348
                  Deletion time for vector: 0.114
                  Deletion time for deque: 0.82
                  

                  使用 gcc 我得到:

                  With gcc I get:

                  Insertion time for list: 5.272
                  Insertion time for vector: 0.125
                  Insertion time for deque: 0.125
                  
                  Deletion time for list: 4.259
                  Deletion time for vector: 0.109
                  Deletion time for deque: 0.109
                  

                  计算出搜索时间的因素有些重要,因为您必须分别计算每次迭代的时间.您需要比 clock (通常是)更精确的东西来产生有意义的结果(更多关于订单或读取时钟周期寄存器).如果您认为合适,请随时对此进行修改——正如我上面提到的,我缺乏动力,因为我看不出这是多么明智的做法.

                  Factoring out the search time would be somewhat non-trivial because you'd have to time each iteration separately. You'd need something more precise than clock (usually is) to produce meaningful results from that (more on the order or reading a clock cycle register). Feel free to modify for that if you see fit -- as I mentioned above, I lack motivation because I can't see how it's a sensible thing to do.

                  这篇关于随机插入/删除的综合向量与链表基准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                • <legend id='ixB7k'><style id='ixB7k'><dir id='ixB7k'><q id='ixB7k'></q></dir></style></legend>

                • <tfoot id='ixB7k'></tfoot>

                    <bdo id='ixB7k'></bdo><ul id='ixB7k'></ul>
                  • <i id='ixB7k'><tr id='ixB7k'><dt id='ixB7k'><q id='ixB7k'><span id='ixB7k'><b id='ixB7k'><form id='ixB7k'><ins id='ixB7k'></ins><ul id='ixB7k'></ul><sub id='ixB7k'></sub></form><legend id='ixB7k'></legend><bdo id='ixB7k'><pre id='ixB7k'><center id='ixB7k'></center></pre></bdo></b><th id='ixB7k'></th></span></q></dt></tr></i><div id='ixB7k'><tfoot id='ixB7k'></tfoot><dl id='ixB7k'><fieldset id='ixB7k'></fieldset></dl></div>

                      <small id='ixB7k'></small><noframes id='ixB7k'>

                              <tbody id='ixB7k'></tbody>