如何拼接字符串效率最高

C++ 拼接长字符串

c++ string 类型提供 opearator+= 以及 append 方法进行字符串拼接,本文探讨c++拼接长字符串执行效率最高的方法。以下是四种实现方式。

实现方式

operator +=

使用 string 类提供重载 += 方法拼接字符串。示例:

// length 参数代表拼接的字符串长度
void composeLongstringWithOperator(const unsigned int length,std::string& long_string)
{
    for (size_t i = 0; i < length / 9; i++)
    {
        char str[10];
        // randStr 方法构造长度为9的随机字符串
        long_string += (randStr(str,9));
    }

}

append

使用 string 类提供的append 方法拼接字符串。示例:

void composeLongstringWithAppend(const unsigned int length,std::string& long_string)
{
    for (size_t i = 0; i < length / 9; i++)
    {
        char str[10];
        long_string.append(randStr(str,9));
    }
}

reserve && operator +=

在拼接字符串之前为string 对象提前分配空间,然后使用 += 方法进行拼接,示例:

void composeLongstringWithReserveAndOperator(const unsigned int length,std::string& long_string)
{
    long_string.reserve(length);
    for (size_t i = 0; i < length / 9; i++)
    {
        char str[10];
        long_string += (randStr(str,9));
    }
}

reserve && append

在拼接字符串之前为string 对象提前分配空间,然后使用 append 方法进行拼接,示例:

void composeLongstringWithAppend(const unsigned int length,std::string& long_string)
{
    for (size_t i = 0; i < length / 9; i++)
    {
        char str[10];
        long_string.append(randStr(str,9));
    }
}

性能测试

测试方法

进行10000次长字符串拼接,统计每种方式下耗时,示例代码如下:

#include <iostream>
#include <string>
#include <ctime>
#include <chrono>
char* randStr(char* str,const int len)
{
    int i;
    for(i = 0; i < len; ++i)
    {
        str[i] = 'A' + rand() % 26;
    }
    str[++i] = '\0';
    return str;
}

int main(int argc, char* argv[])
{
    (void) argc;
    // 第一个参数代表生成的字符串的长度
    const unsigned int length = atoi(argv[1]);
    // 第二个参数代表使用哪种方法进行拼接
    const unsigned int type = atoi(argv[2]);

    srand(time(NULL));
    auto start = std::chrono::high_resolution_clock::now();
    switch(type)
    {
        case 1:
            std::cout << "composeLongstringWithReserveAndAppend";
            for(int i = 0; i < 10000; i++)
            {
                std::string long_string;
                composeLongstringWithReserveAndAppend(length,long_string);
            }
            break;
        case 2:
            std::cout << "composeLongstringWithReserveAndOperator";
            for(int i = 0; i < 10000; i++)
            {
                std::string long_string;
                composeLongstringWithReserveAndOperator(length,long_string);
            }
            break;
        case 3:
            std::cout << "composeLongstringWithAppend";
            for(int i = 0; i < 10000; i++)
            {
                std::string long_string;
                composeLongstringWithAppend(length,long_string);
            }
            break;
        case 4:
            std::cout << "composeLongstringWithOperator";
            for(int i = 0; i < 10000; i++)
            {
                std::string long_string;
                composeLongstringWithOperator(length,long_string);
            }
            break;
        default:
            return 0;
    }
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> diff = end - start;
    std::cout << " cost " << 1000 * diff.count() << " ms\n";

    return 0;
}

编译

g++ -std=c++11 -O3 compose_long_string.cpp -o compose_long_string

性能表现

长字符串长度为1000000,每种方法进行10000次拼接,四种方法的耗时如下:

method cost (ms)
reserve && append 117304
reserve && operator 122998
append 125682
operator 129071

结论

  1. 针对较短字符串,使用reserve提前分配空间对性能提升意义不大,当字符串的长度很长是,使用reserve方法提前分配空间可以带来比较大的性能提升。
  2. operator+=append 方法在进行字符串拼接时性能表现几乎一致。原因是stl 实现的operator+= 方式实际是直接调用了append 方法。
  3. 综上,拼接长字符串时最优方式是 reserve && append