来源于libGod官网http://www.libgod.com/

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

#include <cstdio>   
#include <map>   
#include <vector>   
#include <string>   
#include <sstream>   
   
#include <boost/multi_index_container.hpp>       
#include <boost/multi_index/mem_fun.hpp>         
#include <boost/multi_index/member.hpp>         
#include <boost/multi_index/ordered_index.hpp>   
#include <boost/smart_ptr.hpp>   
   
#include "god/types.h"   
#include "god/util.h"   
#include "god/performance_counter.h"   
   
using namespace std;   
using namespace G;   
   
struct Person {   
    typedef boost::shared_ptr<Person> ptr;   
   
    Person(int _id, std::string _name)   
        : id(_id)   
        , name(_name)   
    {}   
   
    int id;   
    std::string name;   
};   
   
struct id_tag{};                                                   
struct name_tag{};      
   
typedef boost::multi_index_container<   
    Person::ptr,   
    boost::multi_index::indexed_by<   
        boost::multi_index::ordered_unique<   
            boost::multi_index::tag<id_tag>,   
            boost::multi_index::member<Person, int, &Person::id>   
        >,   
        boost::multi_index::ordered_unique<   
            boost::multi_index::tag<name_tag>,   
            boost::multi_index::member<Person, std::string, &Person::name>   
        >   
    >   
> PersonContainer;   
typedef PersonContainer::index<id_tag>::type IdIndex;   
typedef PersonContainer::index<name_tag>::type NameIndex;   
   
typedef std::map<int, Person::ptr> IdPersons;   
typedef std::map<std::string, Person::ptr> NamePersons;   
   
typedef std::vector<Person::ptr> Persons;   
   
int main(int argc, char *argv[]) {   
    if (argc != 2) {   
        printf("Usage: program count\n");   
        return 0;   
    }   
    PersonContainer container;   
    IdPersons idPersons;   
    NamePersons namePersons;   
    printf("container: %d idPersons: %d namePersons: %d\n", sizeof(container), sizeof(idPersons), sizeof(namePersons));   
   
    Persons persons;   
    stringstream ss;   
    int id;   
    std::vector<char> flags;   
    int num = atoi(argv[1]);   
    int max = 10 * num;   
    flags.resize(max, 0);   
    for (int i = 0; i < num; ++i) {   
        do {   
            id = randInt(0, max);   
        } while (flags[id] == 1);   
        flags[id] = 1;   
        ss.str("helloworld");   
        ss << id;   
        persons.push_back(Person::ptr(new Person(id, ss.str())));   
    }   
    printf("---------------插入%d次-----------------\n", persons.size());   
    {   
        TIME_COUNTER_DEFAULT("Container插入");   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            container.insert(*it);   
        }   
    }   
    {   
        TIME_COUNTER_DEFAULT("Map插入");   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            idPersons.insert(IdPersons::value_type((*it)->id, *it));   
            namePersons.insert(NamePersons::value_type((*it)->name, *it));   
        }   
    }   
    printf("---------------查找%d次-----------------\n", persons.size());   
    {   
        TIME_COUNTER_DEFAULT("Container查找");   
        IdIndex &idIndex = container.get<id_tag>();   
        NameIndex &nameIndex = container.get<name_tag>();   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            idIndex.find((*it)->id);   
            nameIndex.find((*it)->name);   
        }   
    }   
    {   
        TIME_COUNTER_DEFAULT("Map查找");   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            idPersons.find((*it)->id);   
            namePersons.find((*it)->name);   
        }   
    }   
    printf("---------------删除%d次-----------------\n", persons.size());   
    {   
        TIME_COUNTER_DEFAULT("Container删除");   
        IdIndex &idIndex = container.get<id_tag>();   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            idIndex.erase(idIndex.find((*it)->id));   
        }   
    }   
    {   
        TIME_COUNTER_DEFAULT("Map删除");   
        for (Persons::iterator it = persons.begin(); it != persons.end(); ++it) {   
            idPersons.erase(idPersons.find((*it)->id));   
            namePersons.erase(namePersons.find((*it)->name));   
        }   
    }   
    return 0;   
}

结果:

./godmulti_index_container.exe 100000   
container: 16 idPersons: 24 namePersons: 24   
---------------插入100000次-----------------   
2013-Jun-07 03:14:04.094847 INFO g:performance god/multi_index_container.cpp:85 main   
RealTime: 0.347873s   
Desc: Container插入   
2013-Jun-07 03:14:04.353037 INFO g:performance god/multi_index_container.cpp:91 main   
RealTime: 0.257946s   
Desc: Map插入   
---------------查找100000次-----------------   
2013-Jun-07 03:14:04.635155 INFO g:performance god/multi_index_container.cpp:99 main   
RealTime: 0.282012s   
Desc: Container查找   
2013-Jun-07 03:14:04.860851 INFO g:performance god/multi_index_container.cpp:108 main   
RealTime: 0.225587s   
Desc: Map查找   
---------------删除100000次-----------------   
2013-Jun-07 03:14:05.083199 INFO g:performance god/multi_index_container.cpp:116 main   
RealTime: 0.221576s   
Desc: Container删除   
2013-Jun-07 03:14:05.335577 INFO g:performance god/multi_index_container.cpp:123 main   
RealTime: 0.251333s   
Desc: Map删除   
./godmulti_index_container.exe 5000000   
container: 16 idPersons: 24 namePersons: 24   
---------------插入5000000次-----------------   
2013-Jun-07 03:41:53.712893 INFO g:performance god/multi_index_container.cpp:85 main   
RealTime: 31.0026s   
Desc: Container插入   
2013-Jun-07 03:42:23.353184 INFO g:performance god/multi_index_container.cpp:91 main   
RealTime: 29.6398s   
Desc: Map插入   
---------------查找5000000次-----------------   
2013-Jun-07 03:42:51.957578 INFO g:performance god/multi_index_container.cpp:99 main   
RealTime: 28.6033s   
Desc: Container查找   
2013-Jun-07 03:43:20.793262 INFO g:performance god/multi_index_container.cpp:108 main   
RealTime: 28.8355s   
Desc: Map查找   
---------------删除5000000次-----------------   
2013-Jun-07 03:43:39.491708 INFO g:performance god/multi_index_container.cpp:116 main   
RealTime: 18.6607s   
Desc: Container删除   
2013-Jun-07 03:44:10.976808 INFO g:performance god/multi_index_container.cpp:123 main   
RealTime: 31.4334s   
Desc: Map删除