【问题标题】:About boost trim function efficiency in multi-thread environment关于在多线程环境下提高修剪功能效率
【发布时间】: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


【解决方案1】:

您没有提供语言环境,因此 trim 将使用当前语言环境。获取当前语言环境可能需要一个锁,这可能会导致您的性能问题。

尝试创建一次区域设置并在所有修剪调用中使用它

std::locale mylocale = std::locale();

...

trim(str, mylocale);

【讨论】:

  • 听起来不错 - 我无法理解 trim() 函数如何“线程敏感”,但如果 trim() 预计可以处理多字符编码,您的回答是一个很好的解释.我应该自己得到它,因为我自己正在将一些遗留的嵌入式东西转换为 BIG5/unicode。获取环境数据的系统调用肯定会比实际的修剪操作花费更长的时间。
  • 我尝试了您的建议,但它不起作用。多线程环境下程序运行时间不变。
猜你喜欢
  • 2021-03-16
  • 2014-02-10
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 2013-04-18
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
相关资源
最近更新 更多