- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 https://github.com/spakai/threadpool_future 中有 3 个针对此代码 ThreadPool 的单元测试用例
class ThreadPoolTest : public Test {
public:
ThreadPool pool;
std::condition_variable wasExecuted;
std::mutex m;
std::vector<std::shared_ptr<std::thread>> threads;
unsigned int count{0};
void incrementCountAndNotify() {
std::unique_lock<std::mutex> lock(m);
++count;
std::cout << count << std::endl;
wasExecuted.notify_all();
}
void waitForNotificationOrFailOnTimeout(unsigned expectedCount, int milliseconds=80000) {
std::unique_lock<std::mutex> lock(m);
ASSERT_THAT(wasExecuted.wait_for(lock, std::chrono::milliseconds(milliseconds), [&] { return count == expectedCount; }), Eq(true));
}
bool hasDuplicates(const std::vector<int> & birthdays) {
std::set<int> uniqueBirthdays(birthdays.begin(), birthdays.end());
return (uniqueBirthdays.size() != birthdays.size());
}
std::vector<int> generateNumbers(const int popSize) {
std::vector<int> list;
std::random_device rd;
std::default_random_engine dre(rd());
std::uniform_int_distribution<int> di(0,365);
for(int i{0}; i < popSize ; i++) {
list.push_back(di(dre));
}
return list;
}
void TearDown() override {
for (auto& t: threads) t->join();
}
};
TEST_F(ThreadPoolTest,TimingTestWithFuture) {
pool.start(4);
std::vector<std::future<unsigned long long>> results;
auto work = [](int n) {
unsigned long long factorial = 1;
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
return factorial;
};
TestTimer timer("4-sized-TP with Future",0);
for (int i = 5; i < 60 ; i++) {
results.push_back(pool.submit(work,i));
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i).get();
}
}
TEST_F(ThreadPoolTest,TimingTestWithCallback) {
pool.start(4);
std::vector<unsigned long long> results;
TestTimer timer("4-sized-TP-Callback",0);
for (int n = 5; n < 60 ; n++) {
auto work = [&]() {
unsigned long long factorial = 1;
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
{
std::lock_guard<std::mutex> guard(m);
results.push_back(factorial);
}
incrementCountAndNotify();
};
pool.add(work);
}
waitForNotificationOrFailOnTimeout(55);
}
TEST_F(ThreadPoolTest,TimingTestWithoutTP) {
std::vector<unsigned long long> results;
auto work = [](int n) {
unsigned long long factorial = 1;
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
return factorial;
};
TestTimer timer("In Sequence",0);
for (int i = 5; i < 60 ; i++) {
results.push_back(work(i));
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i);
}
}
我在一台 4 CPU 机器上运行。我得到的计时结果显示单线程最快,而返回 future 的线程最慢。
4-sized-TP with Future Time taken = 2.364ms
4-sized-TP-Callback 所用时间 = 1.103ms
按顺序花费的时间 = 0.026 毫秒
我原以为时间顺序会倒过来。我做测试的方式是错误的吗?还是我的代码?
新测试会占用 CPU 资源
TEST_F(ThreadPoolTest,BirthdayParadoxInSequenceTimingTest) {
std::vector<int> results;
TestTimer timer("Birthday Paradox :: In Sequence",0);
std::vector<int> popList = {10,23,30,40,50,60,70,80,90,100,120,150};
for(auto it=popList.begin(); it!=popList.end(); ++it) {
int id = *it;
int dup{0};
for(int i{0}; i< 100000; i++) {
auto list = generateNumbers(id);
if(hasDuplicates(list)) ++dup;
}
results.push_back(dup);
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i);
}
}
TEST_F(ThreadPoolTest,BirthdayParadoxTPWithFutureTimingTest) {
std::vector<int> popList = {10,23,30,40,50,60,70,80,90,100,120,150};
pool.start(4);
std::vector<std::future<int>> results;
TestTimer timer("4-sized-TP with Future",0);
for(auto it=popList.begin(); it!=popList.end(); ++it) {
int id = *it;
auto work = [&](int pop) {
int dup{0};
for(int i{0}; i < 100000 ; i++) {
auto list = generateNumbers(pop);
if(hasDuplicates(list)) ++dup;
}
return dup;
};
results.push_back(pool.submit(work,id));
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i).get();
}
}
TEST_F(ThreadPoolTest,BirthdayParadoxTPWithCallBackTimingTest) {
std::vector<int> popList = {10,23,30,40,50,60,70,80,90,100,120,150};
pool.start(4);
std::vector<int> results;
TestTimer timer("4-sized-TP with Callback",0);
for(auto it=popList.begin(); it!=popList.end(); ++it) {
int id = *it;
auto work = [&,id]() {
int dup{0};
for(int i{0}; i < 100000 ; i++) {
auto list = generateNumbers(id);
if(hasDuplicates(list)) ++dup;
{
std::lock_guard<std::mutex> guard(m);
results.push_back(dup);
}
}
incrementCountAndNotify();
};
pool.add(work);
}
waitForNotificationOrFailOnTimeout(12);
}
结果还是出乎我的意料
按顺序花费的时间 = 37555.7ms
4-sized-TP with Future Time taken = 62544.8ms
4-sized-TP with Callback Time = 62563.6ms
完整代码和测试在 https://github.com/spakai/threadpool_future 中
最佳答案
你选择的生日悖论问题对 cpu 来说也不是一个具有挑战性的任务。但是要理解您看到的问题,我们首先必须对代码进行一些更改。
我们想测量算法完成所需的时间。内存分配是昂贵的,应该避免在程序中经常重复的部分。创建 vector 或增加它们的大小总是会触发内存分配。创建集合也是如此。为了删除 momory 分配,我将您的代码修改为如下所示:
#include "gmock/gmock.h"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <random>
#include <set>
#include <vector>
#include "ThreadPool.h"
#include "TestTimer.h"
const unsigned int runs = 100000;
using namespace testing;
class ThreadPoolTest : public Test {
public:
ThreadPool pool;
std::condition_variable wasExecuted;
std::mutex m;
std::mutex n;
std::vector<std::shared_ptr<std::thread>> threads;
std::vector<int> popList = {10,11,12,23};
unsigned int count{0};
void incrementCountAndNotify() {
{
std::unique_lock<std::mutex> lock(m);
++count;
}
wasExecuted.notify_all();
}
void waitForNotificationOrFailOnTimeout(unsigned expectedCount, int milliseconds=80000) {
std::unique_lock<std::mutex> lock(m);
ASSERT_THAT(wasExecuted.wait_for(lock, std::chrono::milliseconds(milliseconds), [&] { return count == expectedCount; }), Eq(true));
}
bool hasDuplicates(const std::vector<int> & birthdays) {
//This way to check for duplicates is very expensive, since it allocates new memory and copies all values around
//std::set<int> uniqueBirthdays(birthdays.begin(), birthdays.end());
//return (uniqueBirthdays.size() != birthdays.size());
for(unsigned int i = 0; i < birthdays.size(); i++) {
for(unsigned int j = i+1; j < birthdays.size(); j++) {
if(birthdays[i]==birthdays[j]) return true;
}
}
return false;
}
//I added the parameter list, to avoid the allocation of new memory
//The list will also have the needed size, so that we dont need to it here
std::vector<int> generateNumbers(std::vector<int>& list) {
//It is not exactly specified how the random_device works, it may read from /dev/random, which can not be done in parallel
//To make the measurements compareable over multiple machiens i removed this code
//std::random_device rd;
std::default_random_engine dre(0);
std::uniform_int_distribution<int> di(0,365);
int counter = 0;
for(int& i : list) {
i = di(dre);
}
return list;
}
void TearDown() override {
for (auto& t: threads) t->join();
}
};
TEST_F(ThreadPoolTest,BirthdayParadoxInSequenceTimingTest) {
std::vector<int> results;
TestTimer timer("Birthday Paradox :: In Sequence",0);
for(auto it=popList.begin(); it!=popList.end(); ++it) {
std::cout << "TID " << std::this_thread::get_id() << std::endl;
int id = *it;
int dup{0};
std::vector<int> list(id); //Allocate memory in the right size only once for all 100000 runs
for(int i{0}; i < runs ; i++) {
generateNumbers(list);
if(hasDuplicates(list)) ++dup;
}
results.push_back(dup); //This push_back is ok, since it is only called 4 times in total
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i);
}
}
TEST_F(ThreadPoolTest,BirthdayParadoxTPWithFutureTimingTest) {
pool.start(4);
std::vector<std::future<int>> results;
TestTimer timer("4-sized-TP with Future",0);
for(auto it=popList.begin(); it!=popList.end(); ++it) {
int id = *it;
auto work = [&](int pop) {
std::cout << "TID " << std::this_thread::get_id() << std::endl;
int dup{0};
std::vector<int> list(pop); //Same as above
for(int i{0}; i < runs ; i++) {
generateNumbers(list);
if(hasDuplicates(list)) ++dup;
}
return dup;
};
results.push_back(pool.submit(work,id));
}
for(unsigned int i = 0; i< results.size(); i++) {
results.at(i).get();
}
}
TEST_F(ThreadPoolTest,BirthdayParadoxTPWithCallBackTimingTest) {
pool.start(4);
std::vector<int> results;
TestTimer timer("4-sized-TP with Callback",0);
for(auto it=popList.begin(); it!=popList.end(); ++it) {
int id = *it;
auto work = [&,id]() {
std::cout << "TID " << std::this_thread::get_id() << std::endl;
int dup{0};
std::vector<int> list(id); //Same here too
for(int i{0}; i < runs ; i++) {
generateNumbers(list);
if(hasDuplicates(list)) ++dup;
{
std::lock_guard<std::mutex> guard(n);
results.push_back(dup);
}
}
incrementCountAndNotify();
};
pool.add(work);
}
waitForNotificationOrFailOnTimeout(4);
}
现在,我们的内存管理正确了,我们可以开始推理运行时了。我使用 2 个内核和超线程运行代码,因此如果我们使用多线程,我们期望加速为 2 或更高。让我们看看结果:
Birthday Paradox :: In Sequence Time taken = 680.96ms
4-sized-TP with Future Time taken = 1838.28ms
4-sized-TP with Callback Time taken = 1861.07ms
如果我将线程池中的线程数量限制为一个,那么所有版本的运行时间几乎相同。
我们看到这种不直观行为的原因是,问题是内存限制的。速度下降的原因在于检查重复项。
for(unsigned int i = 0; i < birthdays.size(); i++) {
for(unsigned int j = i+1; j < birthdays.size(); j++) {
if(birthdays[i]==birthdays[j]) return true;
}
}
生日的访问在内存中很好地对齐。如果有多个线程在运行,则算法不会提高速度,因为所有线程都只是在等待值。更糟糕的是,不同的线程正在从不同的位置读取,因此,它们可能会丢弃可能被其他线程使用的缓存行。这就是性能下降的原因。
关于c++ - 线程池 : single thread vs callback tp vs future tp 的计时测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43860261/
在下面的方法中,如何将第一个Single的结果传递给第二个Single? 如现在所写,当它返回时,somethingId 是空的。如果我将返回的 Single.just(somethingId) 中的
我发现很多帖子都在回答类似的问题(例如“如何用 / 替换 \”或“如何替换 \\” by \”。我理解所有这些,但没有一个能解决我的特殊问题。这里是: 我正在从注册表中读取路径字符串,其中包含“///
注意:事实证明,toCompletable() 并非错误,而是调用代码。调用代码使得更改此方法使其工作(或不工作)。 我有以下方法。它返回一个 Single。有用。执行内部代码,以便 remoteDa
react-native repo here 中的一个文件中有一段代码如下图: export type Operation = & {instanceID: DebugID} & (
当使用带有 Single() 的 LINQ 时,我的代码行总是带有绿色下划线,并带有建议“替换为对 single 的单一调用”。这是什么意思?下面是产生该建议的一行代码示例: var user = d
讨论来自 this answer让我好奇。哪个更快: someEnumerable.Single(predicate); 或 someEnumerable.Where(predicate).Singl
我正在使用 Keycloak 作为单点登录 (SSO) 平台的 OP。我已经将我的两个 Web 应用程序连接到 Keycloak,以便使用单点登录功能。 此外,我已经制作了一个应用程序,当注销时将被重
我的步骤是: 创建单个值 x - 可能会占用一些 CPU 资源 使用值x来执行IO操作。这已经返回 Completable 返回x 所以我想这样做: Single result =
我想知道是否有人可以阐明这个问题,什么时候使用 Single.fromCallable( ()-> myObject ) 代替 Single.just(myObject) 根据文档,Single.fr
我有两个 Singles 来源,我将它们组合成一个 Single of Pair。 假设我们对这些来源有两种方法: private Single single1() {} private Single
我想将单个 Intel CPU 内核的速度与单个 nVidia GPU 内核的速度(即:单个 CUDA 代码、单个线程)进行比较。我确实实现了以下简单的二维图像卷积算法: void convoluti
我在实现 Ping Federate 时遇到此问题 Error - Single Sign-On Single sign-on authentication was unsuccessful (ref
我有几个 api 调用(Rx singles),我想将它们组合成一个 Single。我正在使用 Single.merge 尝试合并这些调用的结果,但是当我订阅响应时,我得到一个空数组,因为订阅已经发生
早上好。我的代码有问题 bootsfaces 。我需要我的 DataTable 支持单行选择,但不支持多行选择。但是,我的表格始终只使用多项选择。 这是我的代码: 我没有进行简单的选择,因为我引用了
我怎样才能像下面的代码那样使用字符串。 $str = 'Is yo"ur name O'reil"ly?'; 上面的代码只是一个例子..我需要使用包含单引号和双引号的大 html 模板。我尝试了 Ad
我有一组地理空间+时间数据和一些附加属性,我将在 map 上显示这些数据。该集合目前有几百万份文件,并且会随着时间的推移而增加。 每个文档都有以下字段: 位置:[geojson 对象] 日期:[日期对
我目前在 .NET 2.0 下使用 SharpZipLib,通过它我需要将单个文件压缩为单个压缩存档。为此,我目前正在使用以下内容: string tempFilePath = @"C:\Users\
我有 table create table1( column1 number(10, column2 number(10), column3 number(10) ); column1是主
考虑下面这段代码,我正在尝试使用 Executors.newFixedThreadPool(1).asCoroutineDispatcher()创建单线程调度程序;我想要 launch(singleT
我面临着困惑,举个例子 4 Single: val s1 : Single = service1.execute().subscribeOn(io()) val s2 : Single = servi
我是一名优秀的程序员,十分优秀!