【发布时间】:2012-06-20 01:08:22
【问题描述】:
我使用了boost trim功能,发现它在单线程环境下表现非常好。
但是当我在多线程环境中调用trim函数时,它的性能会很差。 我也发现使用多进程方式调用它会有很好的性能。
最后,我写了一个简单的trim函数,它在多线程环境或多进程环境下都表现的很好。
我想我一定是在多线程环境下用错了。 所以我想知道怎么了。
感谢您的回复。
增强版本:增强 1.46.1 操作系统:linux redhat 6.1,8核,24G内存。
打击是示例代码 test1.cpp,多线程环境调用trim函数
//----------------------------
---------------------
using namespace std;
using namespace boost;
void *TrimNString(void *arg) {
string base ="fdsffdsafdsa";
for(int i = 0; i != 50000000;i++)
{
string str = base;
trim(str);
}
return 0;
}
int main()
{
//8 threads to call trim function
system("date");
pthread_t mythread1, mythread2, mythread3, mythread4, mythread5, mythread6, mythread7,mythread8;
pthread_create(&mythread1, NULL, TrimNString, NULL);
pthread_create(&mythread2, NULL, TrimNString, NULL);
pthread_create(&mythread3, NULL, TrimNString, NULL);
pthread_create(&mythread4, NULL, TrimNString, NULL);
pthread_create(&mythread5, NULL, TrimNString, NULL);
pthread_create(&mythread6, NULL, TrimNString, NULL);
pthread_create(&mythread7, NULL, TrimNString, NULL);
pthread_create(&mythread8, NULL, TrimNString, NULL);
pthread_join(mythread1, NULL);
pthread_join(mythread2, NULL);
pthread_join(mythread3, NULL);
pthread_join(mythread4, NULL);
pthread_join(mythread5, NULL);
pthread_join(mythread6, NULL);
pthread_join(mythread7, NULL);
pthread_join(mythread8, NULL);
system("date");
return 0;
}
test2.cpp,多进程环境调用trim函数
//-------------------------------------------------
/*
* test.cpp
*
* Created on: 2012-6-19
* Author: root
*/
#include<pthread.h>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
using namespace boost;
void *TrimNString(void *arg) {
system("./TrimNString");// TrimNString is produced by test3.cpp
return 0;
}
int main()
{
//8 process to call trim function
system("date");
pthread_t mythread1, mythread2, mythread3, mythread4, mythread5, mythread6, mythread7,mythread8;
pthread_create(&mythread1, NULL, TrimNString, NULL);
pthread_create(&mythread2, NULL, TrimNString, NULL);
pthread_create(&mythread3, NULL, TrimNString, NULL);
pthread_create(&mythread4, NULL, TrimNString, NULL);
pthread_create(&mythread5, NULL, TrimNString, NULL);
pthread_create(&mythread6, NULL, TrimNString, NULL);
pthread_create(&mythread7, NULL, TrimNString, NULL);
pthread_create(&mythread8, NULL, TrimNString, NULL);
pthread_join(mythread1, NULL);
pthread_join(mythread2, NULL);
pthread_join(mythread3, NULL);
pthread_join(mythread4, NULL);
pthread_join(mythread5, NULL);
pthread_join(mythread6, NULL);
pthread_join(mythread7, NULL);
pthread_join(mythread8, NULL);
system("date");
return 0;
}
test3.cpp,test2.cpp的可执行文件
/*
* test.cpp
*
* Created on: 2012-6-19
* Author: root
*/
#include<pthread.h>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
using namespace boost;
//produce the executable file
int main()
{
string base ="fdsffdsafdsa";
for(int i = 0; i != 50000000;i++)
{
string str = base;
trim(str);
}
return 0;
}
test4.cpp,在多线程环境下调用一个简单的trim(不是boost库)函数,性能和多进程调用差不多。
//-------------------------------------------------
#include<pthread.h>
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
using namespace boost;
void ltrim(string & str)
{
if(str.find_first_not_of(" \n\r\t") != string::npos)
{
str = str.substr(str.find_first_not_of(" \n\r\t"));
}
}
void rtrim(string & str)
{
if(str.find_first_not_of(" \n\r\t") != string::npos)
{
str = str.substr(0, str.find_last_not_of(" \n\r\t") + 1);
}
}
void trimStr(string &str)
{
ltrim(str);
rtrim(str);
}
void *TrimNString(void *arg) {
string base ="fdsffdsafdsa";
for(int i = 0; i != 50000000;i++)
{
string str = base;
trimStr(str);
}
return 0;
}
int main()
{
//8 threads to call trim function
system("date");
pthread_t mythread1, mythread2, mythread3, mythread4, mythread5, mythread6, mythread7,mythread8;
pthread_create(&mythread1, NULL, TrimNString, NULL);
pthread_create(&mythread2, NULL, TrimNString, NULL);
pthread_create(&mythread3, NULL, TrimNString, NULL);
pthread_create(&mythread4, NULL, TrimNString, NULL);
pthread_create(&mythread5, NULL, TrimNString, NULL);
pthread_create(&mythread6, NULL, TrimNString, NULL);
pthread_create(&mythread7, NULL, TrimNString, NULL);
pthread_create(&mythread8, NULL, TrimNString, NULL);
pthread_join(mythread1, NULL);
pthread_join(mythread2, NULL);
pthread_join(mythread3, NULL);
pthread_join(mythread4, NULL);
pthread_join(mythread5, NULL);
pthread_join(mythread6, NULL);
pthread_join(mythread7, NULL);
pthread_join(mythread8, NULL);
system("date");
return 0;
}
【问题讨论】:
-
..看起来更像是对字符串 ctor 的性能测试,而不是任何修剪。修剪字符串有什么困难,一旦字符串实际存在,一些去除前导/尾随空格的代码无论如何都不应该是多线程敏感的。
-
我使用了很多提升字符串函数。我不知道其他 boost 算法函数在多线程环境中也有同样的问题。如果只是修剪功能有问题,我可以更换修剪功能。但我不确定。
-
您是否尝试过实施您从 Boost ML 获得的建议? thread.gmane.org/gmane.comp.lib.boost.user/74528/focus=74532你的结论是什么?
标签: multithreading performance boost trim multiprocess