zoukankan      html  css  js  c++  java
  • multi_index_container性能测试

    boost中有个multi_index_container,感觉比较好用,但不知道性能怎么样。今天特意测试了下他的插入,查找,删除的性能。

    测试代码:

     
    1. #include <cstdio>   
    2.   
    3. #include <map>   
    4. #include <vector>   
    5. #include <string>   
    6. #include <sstream>   
    7.   
    8. #include <boost/multi_index_container.hpp>       
    9. #include <boost/multi_index/mem_fun.hpp>         
    10. #include <boost/multi_index/member.hpp>         
    11. #include <boost/multi_index/ordered_index.hpp>   
    12. #include <boost/smart_ptr.hpp>   
    13.   
    14. #include "god/types.h"   
    15. #include "god/util.h"   
    16. #include "god/performance_counter.h"   
    17.   
    18. using namespace std;   
    19. using namespace G;   
    20.   
    21. struct Person {   
    22.     typedef boost::shared_ptr<Person> ptr;   
    23.   
    24.     Person(int _id, std::string _name)   
    25.         : id(_id)   
    26.         , name(_name)   
    27.     {}   
    28.   
    29.     int id;   
    30.     std::string name;   
    31. };   
    32.   
    33. struct id_tag{};                                                   
    34. struct name_tag{};      
    35.   
    36. typedef boost::multi_index_container<   
    37.     Person::ptr,   
    38.     boost::multi_index::indexed_by<   
    39.         boost::multi_index::ordered_unique<   
    40.             boost::multi_index::tag<id_tag>,   
    41.             boost::multi_index::member<Person, int, &Person::id>   
    42.         >,   
    43.         boost::multi_index::ordered_unique<   
    44.             boost::multi_index::tag<name_tag>,   
    45.             boost::multi_index::member<Person, std::string, &Person::name>   
    46.         >   
    47.     >   
    48. > PersonContainer;   
    49. typedef PersonContainer::index<id_tag>::type IdIndex;   
    50. typedef PersonContainer::index<name_tag>::type NameIndex;   
    51.   
    52. typedef std::map<int, Person::ptr> IdPersons;   
    53. typedef std::map<std::string, Person::ptr> NamePersons;   
    54.   
    55. typedef std::vector<Person::ptr> Persons;   
    56.   
    57. int main(int argc, char *argv[]) {   
    58.     if (argc != 2) {   
    59.         printf("Usage: program count ");   
    60.         return 0;   
    61.     }   
    62.     PersonContainer container;   
    63.     IdPersons idPersons;   
    64.     NamePersons namePersons;   
    65.     printf("container: %d idPersons: %d namePersons: %d "sizeof(container), sizeof(idPersons), sizeof(namePersons));   
    66.   
    67.     Persons persons;   
    68.     stringstream ss;   
    69.     int id;   
    70.     std::vector<char> flags;   
    71.     int num = atoi(argv[1]);   
    72.     int max = 10 * num;   
    73.     flags.resize(max, 0);   
    74.     for (int i = 0; i < num; ++i) {   
    75.         do {   
    76.             id = randInt(0, max);   
    77.         } while (flags[id] == 1);   
    78.         flags[id] = 1;   
    79.         ss.str("helloworld");   
    80.         ss << id;   
    81.         persons.push_back(Person::ptr(new Person(id, ss.str())));   
    82.     }   
    83.     printf("---------------插入%d次----------------- ", persons.size());   
    84.     {   
    85.         TIME_COUNTER_DEFAULT("Container插入");   
    86.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    87.             container.insert(*it);   
    88.         }   
    89.     }   
    90.     {   
    91.         TIME_COUNTER_DEFAULT("Map插入");   
    92.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    93.             idPersons.insert(IdPersons::value_type((*it)->id, *it));   
    94.             namePersons.insert(NamePersons::value_type((*it)->name, *it));   
    95.         }   
    96.     }   
    97.     printf("---------------查找%d次----------------- ", persons.size());   
    98.     {   
    99.         TIME_COUNTER_DEFAULT("Container查找");   
    100.         IdIndex &idIndex = container.get<id_tag>();   
    101.         NameIndex &nameIndex = container.get<name_tag>();   
    102.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    103.             idIndex.find((*it)->id);   
    104.             nameIndex.find((*it)->name);   
    105.         }   
    106.     }   
    107.     {   
    108.         TIME_COUNTER_DEFAULT("Map查找");   
    109.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    110.             idPersons.find((*it)->id);   
    111.             namePersons.find((*it)->name);   
    112.         }   
    113.     }   
    114.     printf("---------------删除%d次----------------- ", persons.size());   
    115.     {   
    116.         TIME_COUNTER_DEFAULT("Container删除");   
    117.         IdIndex &idIndex = container.get<id_tag>();   
    118.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    119.             idIndex.erase(idIndex.find((*it)->id));   
    120.         }   
    121.     }   
    122.     {   
    123.         TIME_COUNTER_DEFAULT("Map删除");   
    124.         for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
    125.             idPersons.erase(idPersons.find((*it)->id));   
    126.             namePersons.erase(namePersons.find((*it)->name));   
    127.         }   
    128.     }   
    129.     return 0;   
    130. }   

    结果:

    1. ./godmulti_index_container.exe 100000   
    2. container: 16 idPersons: 24 namePersons: 24   
    3. ---------------插入100000次-----------------   
    4. 2013-Jun-07 03:14:04.094847 INFO g:performance god/multi_index_container.cpp:85 main   
    5. RealTime: 0.347873s   
    6. Desc: Container插入   
    7. 2013-Jun-07 03:14:04.353037 INFO g:performance god/multi_index_container.cpp:91 main   
    8. RealTime: 0.257946s   
    9. Desc: Map插入   
    10. ---------------查找100000次-----------------   
    11. 2013-Jun-07 03:14:04.635155 INFO g:performance god/multi_index_container.cpp:99 main   
    12. RealTime: 0.282012s   
    13. Desc: Container查找   
    14. 2013-Jun-07 03:14:04.860851 INFO g:performance god/multi_index_container.cpp:108 main   
    15. RealTime: 0.225587s   
    16. Desc: Map查找   
    17. ---------------删除100000次-----------------   
    18. 2013-Jun-07 03:14:05.083199 INFO g:performance god/multi_index_container.cpp:116 main   
    19. RealTime: 0.221576s   
    20. Desc: Container删除   
    21. 2013-Jun-07 03:14:05.335577 INFO g:performance god/multi_index_container.cpp:123 main   
    22. RealTime: 0.251333s   
    23. Desc: Map删除   
    1. ./godmulti_index_container.exe 5000000   
    2. container: 16 idPersons: 24 namePersons: 24   
    3. ---------------插入5000000次-----------------   
    4. 2013-Jun-07 03:41:53.712893 INFO g:performance god/multi_index_container.cpp:85 main   
    5. RealTime: 31.0026s   
    6. Desc: Container插入   
    7. 2013-Jun-07 03:42:23.353184 INFO g:performance god/multi_index_container.cpp:91 main   
    8. RealTime: 29.6398s   
    9. Desc: Map插入   
    10. ---------------查找5000000次-----------------   
    11. 2013-Jun-07 03:42:51.957578 INFO g:performance god/multi_index_container.cpp:99 main   
    12. RealTime: 28.6033s   
    13. Desc: Container查找   
    14. 2013-Jun-07 03:43:20.793262 INFO g:performance god/multi_index_container.cpp:108 main   
    15. RealTime: 28.8355s   
    16. Desc: Map查找   
    17. ---------------删除5000000次-----------------   
    18. 2013-Jun-07 03:43:39.491708 INFO g:performance god/multi_index_container.cpp:116 main   
    19. RealTime: 18.6607s   
    20. Desc: Container删除   
    21. 2013-Jun-07 03:44:10.976808 INFO g:performance god/multi_index_container.cpp:123 main   
    22. RealTime: 31.4334s   
    23. Desc: Map删除   
  • 相关阅读:
    Django之数据库表的创建和ORM相关操作
    Django后续和Ajax初识
    阿里云Maven中央仓库配置
    java/javascript 时间操作工具类
    原生javascript实现文件异步上传
    MySQL中的存储函数和存储过程的简单示例
    java同步锁的正确使用
    浅谈javascript的面向对象思想
    java与javascript对cookie操作的工具类
    json字符串与json对象的相互转换
  • 原文地址:https://www.cnblogs.com/libgod/p/3231794.html
Copyright © 2011-2022 走看看