开发者

Is there any speed test between "std::map with mutexes" vs "libcds maps (Michael Hashmap and Split Order List)" parallel insert, find, erase?

So I really would like to see some speed testing of parallel (something like from 100 to 10000 parallel threads) where each thread does insert, find, delete on at least 3 types of concurrent maps - std::map (with some mutexes) vs libcds (Concurrent Data Structures)...

So for example if such comparison does not already exist, please help me with creating one.

Directly Related: LibCds: Michael Hashmap and Split Order List

Imagine we have

#include <iostream>
#include <boost/thread.hpp>
#include <map>

class TestDs 
{
public:

    virtual bool containsKey(int key)=0;
    virtual int get(int key)=0;
    virtual int put(int key, int value)=0;
    virtual int remove(int key)=0;

    virtual int size()=0;
    virtual const char* name()=0;
    virtual void print()=0;
    virtual void shutdown()=0;
};

class GeneralMap: public TestDs
{
private:

    std::map<int,int> _ds;
    mutable boost::mutex mut_;
public:
    GeneralMap() {}

    bool containsKey(int key) {
        boost::mutex::scoped_lock lock(mut_);
        if ( _ds.find(key) != _ds.end())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int get(int key) {
        boost::mutex::scoped_lock lock(mut_);
        return _ds[key];
    }

    int put(int key, int value) {
        boost::mutex::scoped_lock lock(mut_);
        _ds.insert(std::pair<int, int>(key,value));
        return key;
    }

    int remove(int key) {
        boost::mutex::scoped_lock lock(mut_);
        return _ds.erase(key);
    }

    int size() {
        boost::mutex::scoped_lock lock(mut_);
        return _ds.size();
    }
    const char* name() {
        return "StdMap";
    }
    void print() {}
    void shutdown() {}

};

than how to create such test that would create N threads and each thread will callcreate, find delete... I started writing something but it compilable code with boost 1.47.0 now...

#include <iostream>
#include <boost/thread.hpp>
#include <map>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>
#include <boost/date_time.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/random.hpp>
#include <boost/progress.hpp>

class timer 
{ 
public: 
    timer() : start_time_(boost::posix_time::microsec_clock::local_time()) {} 

    void restart() 
    {
        start_time_ = boost::posix_time::microsec_clock::local_time();
    } 

    boost::posix_time::time_duration elapsed() const 
    {
        return boost::posix_time::microsec_clock::local_time() - start_time_;
    } 
private: 
    boost::posix_time::ptime start_time_; 
};

class TestDs 
{
public:

    virtual bool containsKey(int key)=0;
    virtual int get(int key)=0;
    virtual int put(int key, int value)=0;
    virtual int remove(int key)=0;

    virtual int size()=0;
    virtual const char* name()=0;
    virtual void print()=0;
    virtual void shutdown()=0;
};

class GeneralMap: public TestDs
{
private:

    std::map<int,int> _开发者_StackOverflow社区ds;
    mutable boost::mutex mut_;
public:
    GeneralMap() {}

    bool containsKey(int key) {
        boost::mutex::scoped_lock lock(mut_);
        if ( _ds.find(key) != _ds.end())
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    int get(int key) {
        boost::mutex::scoped_lock lock(mut_);
        return _ds[key];
    }

    int put(int key, int value) {
        boost::mutex::scoped_lock lock(mut_);
        _ds.insert(std::pair<int, int>(key,value));
        return key;
    }

    int remove(int key) {
        boost::mutex::scoped_lock lock(mut_);
        return _ds.erase(key);
    }

    int size() {
        boost::mutex::scoped_lock lock(mut_);
        return _ds.size();
    }
    const char* name() {
        return "StdMap";
    }
    void print() {}
    void shutdown() {}

};

template <class map_wraper_t>
class test_map_wraper
{
public:

    test_map_wraper(int threads_number)
    {
        n = threads_number;
    }

    void start_tests()
    {

        boost::upgrade_lock<boost::shared_mutex> lock(tests);
        boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);

        boost::shared_lock<boost::shared_mutex> lock_r(results);

        for(int i=0; i<n; i++)
        {
            boost::thread worker(&test_map_wraper::test, this, i);
        }
        boost::thread worker_r(&test_map_wraper::result, this);
        timerForCaptureFame.restart();
    }
private:
    int n;
    boost::shared_mutex  tests;
    boost::shared_mutex  results;
    boost::random::mt19937 rng;
    timer timerForCaptureFame;
    map_wraper_t Ds;
    boost::progress_display *show_progress;

    void test( int i)
    {
        boost::shared_lock<boost::shared_mutex> lock_r(results);
        boost::shared_lock<boost::shared_mutex> lock(tests);
        Ds.put(i, 0);
        if (Ds.containsKey(i))
        {
            Ds.get(i);
        }
        Ds.remove(i);
    }

    void result()
    {
        boost::upgrade_lock<boost::shared_mutex> lock(results);
        boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);

        std::cout <<  std::endl << "test of " << Ds.name() << " complite;" << std::endl << "test performed on " << n << " items" << std::endl << "test duration: " << timerForCaptureFame.elapsed() << std::endl;
    }
};


int main()
{
    int threads_n = 1000;
    int tests = 5;
    std::cout << "Number of required tests: " << tests << std::endl << "Number of threads in each test: " << threads_n << std::endl << "Wait for it..." << std::endl;
    //for(int i = 0; i < tests; ++i)
    //{
        test_map_wraper<GeneralMap> GeneralMapTest(threads_n);
        GeneralMapTest.start_tests();
    //}
    std::cin.get();
    return 0;
}


Yes, it is in the unit tests for LibCds

It will run the same test scenarios with all kinds of different map types, including your lock-free implementations, but also std::map/set with and without locking.

Of course, no synchronisation is clearly the winner, but it won't work when there are writers in the mix. The lock-free implementations come out a lot faster than the STL containers with synchronized access.

All of this should not be a surprise. Why are you asking?

PS. the unit test are here:

  • get tar ball
  • extract
  • build (cd build && ./build.sh)
  • unit tests in ./bin/*/cds-unit or ./bin/*/cds-unit-debug if built with --debug-test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜