jjzjj

c++ - "std::map with mutexes"与 "libcds maps (Michael Hashmap and Split Order List)"并行插入、查找、删除之间是否有任何速度测试?

coder 2024-02-23 原文

所以我真的很想看到一些并行的速度测试(比如从 100 到 10000 个并行线程),其中每个线程至少在 3 种类型的并发映射上插入、查找、删除 - std::map(有一些互斥锁)与 libcds (Concurrent Data Structures) ...

例如,如果这样的比较尚不存在,请帮助我创建一个。

直接相关: LibCds: Michael Hashmap and Split Order List

假设我们有

#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() {}

};

比起如何创建这样的测试,它会创建 N 个线程,每个线程都会调用创建、查找删除...我开始写一些东西,但它现在可以用 boost 1.47.0 编译代码...

#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> _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;
}

最佳答案

是的,它在 LibCds 的单元测试中

它将使用各种不同的 map 类型运行相同的测试场景,包括您的无锁实现,还有带锁和不带锁的 std::map/set。

当然,没有同步显然是赢家,但是当混合中有编写器时它就不起作用了。无锁实现比具有同步访问的 STL 容器快得多。

所有这些都不足为奇。你为什么要问?

附言。单元测试在这里:

  • 获取 tar 球
  • 摘录
  • 构建(cd build && ./build.sh)
  • ./bin/*/cds-unit./bin/*/cds-unit-debug 中进行单元测试(如果使用 --debug 构建) -测试

关于c++ - "std::map with mutexes"与 "libcds maps (Michael Hashmap and Split Order List)"并行插入、查找、删除之间是否有任何速度测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216604/

有关c++ - "std::map with mutexes"与 "libcds maps (Michael Hashmap and Split Order List)"并行插入、查找、删除之间是否有任何速度测试?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  8. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  9. ruby - 我可以使用 aws-sdk-ruby 在 AWS S3 上使用事务性文件删除/上传吗? - 2

    我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的

  10. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

随机推荐