1

我使用了 boost trim 功能,发现它在单线程环境中表现得非常好。

但是当我在多线程环境中调用trim函数时,它的性能会很差。我还发现使用多进程方法调用它会有很好的性能。

最后,我写了一个简单的trim函数,它在多线程环境或多进程环境下都表现的很好。

我想我必须在多线程环境中错误地使用它。所以我想知道出了什么问题。

感谢您的回复。

boost版本:boost 1.46.1 操作系统:linux redhat 6.1,8core,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;
}
4

1 回答 1

2

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

尝试创建一次语言环境并在所有修剪调用中使用它

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

...

trim(str, mylocale);
于 2012-06-20T06:25:55.483 回答