关于多线程环境中的提升修剪功能效率

时间:2012-06-20 01:08:23

标签: multithreading performance boost trim multiprocess

我使用boost trim函数,发现它在单线程环境中表现非常好。

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

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

我认为我必须在多线程环境中错误地使用它。 所以我想知道什么是错的。

感谢您的回复。

提升版本:提升1.46.1 os: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;
}

1 个答案:

答案 0 :(得分:2)

您没有提供区域设置,因此trim将使用当前区域设置。获取当前区域设置可能需要锁定,这可能会导致性能问题。

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

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

...

trim(str, mylocale);