fmt::format 性能测试

fmt::format 性能测试

结论

O0 下
fmt 比直接用 string 相加快很多,略弱于reserve 后 append

O2 下
fmt 是最快的,append反而是最慢的

无脑用fmt

测试代码

#include <iostream>
#include <string>
#include <fmt/format.h>
#include <gtest/gtest.h>
const int N = 10000000;
std::string a{"hello"}, b{"world"}, c{"end"};
TEST(fmtTest, fmt) {

    // std::string a{"hello"}, b{"world"}, c{"end"};
    for(int i = 0; i < N; i++) {
        auto d = fmt::format("%s_%s_%s", a, b, c);
    }
}
TEST(stringTest, add) {
    // std::string a{"hello"}, b{"world"}, c{"end"};
    for(int i = 0; i < N; i++) {
        auto d = a + "_" + b + "_" + c;
    }
}
TEST(charTest, add) {
    // std::string a{"hello"}, b{"world"}, c{"end"};
    for(int i = 0; i < N; i++) {
        std::string s;
        s.resize(a.size() + b.size() + c.size() + 2);
        for(int j = 0; j < a.size(); j++) {
            s[j] = a[j];
        }
        s[a.size()] = '_';
        int base = a.size() + 1;
        for(int j = 0; j < b.size(); j++) {
            s[base + j] = b[j];
        }
        s[base + b.size()] = '_';
        base += b.size() + 1;
        for(int j = 0; j < c.size(); j++) {
            s[base + j] = c[j];
        }
    }
}
TEST(stirngTest, append) {
    // std::string a{"hello"}, b{"world"}, c{"end"};
    for(int i = 0; i < N; i++) {
        std::string s;
        s.reserve(a.size() + b.size() + 1);
        s.append(a);
        s.append("_");
        s.append(b);
        s.append("_");
        s.append(c);
    }
}
int main(int argc, char** argv) {
    srand(time(0));
    a += rand() % 26 + 'a';
    b += rand() % 26 + 'a';
    c += rand() % 26 + 'a';
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

output

  • O0
[==========] Running 4 tests from 4 test suites.
[----------] Global test environment set-up.
[----------] 1 test from fmtTest
[ RUN      ] fmtTest.fmt
[       OK ] fmtTest.fmt (1787 ms)
[----------] 1 test from fmtTest (1787 ms total)

[----------] 1 test from stringTest
[ RUN      ] stringTest.add
[       OK ] stringTest.add (3742 ms)
[----------] 1 test from stringTest (3742 ms total)

[----------] 1 test from charTest
[ RUN      ] charTest.add
[       OK ] charTest.add (6422 ms)
[----------] 1 test from charTest (6422 ms total)

[----------] 1 test from stirngTest
[ RUN      ] stirngTest.append
[       OK ] stirngTest.append (1663 ms)
[----------] 1 test from stirngTest (1663 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 4 test suites ran. (13617 ms total)
[  PASSED  ] 4 tests.
  • O2
[==========] Running 4 tests from 4 test suites.
[----------] Global test environment set-up.
[----------] 1 test from fmtTest
[ RUN      ] fmtTest.fmt
[       OK ] fmtTest.fmt (254 ms)
[----------] 1 test from fmtTest (254 ms total)

[----------] 1 test from stringTest
[ RUN      ] stringTest.add
[       OK ] stringTest.add (374 ms)
[----------] 1 test from stringTest (374 ms total)

[----------] 1 test from charTest
[ RUN      ] charTest.add
[       OK ] charTest.add (268 ms)
[----------] 1 test from charTest (268 ms total)

[----------] 1 test from stirngTest
[ RUN      ] stirngTest.append
[       OK ] stirngTest.append (472 ms)
[----------] 1 test from stirngTest (472 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 4 test suites ran. (1370 ms total)
[  PASSED  ] 4 tests.

汇编比较

  • append
    https://godbolt.org/z/vM77o3898

  • char add
    https://godbolt.org/z/7jYYE78rs

猜测循环变快的原因是编译器帮我们做了循环展开

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值