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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值