C++备忘录071:benchmark 说传参时perfect forwarding的效率一般最好,move相对copy是可以有额外代价的

struct foo {
   
  foo(paramType s) : s{
   s} {
   }
  std::string s;
};

perfect forwarding"最丑陋",但绝大多数情况下效率最高

因为要处理源对象,所以在不涉及动态内存分配的情况下,相对单纯拷贝来说,移动的代价更高

/*

`c` for copy/create, `m` for move

*_s_* passes a string argument
*_l_* passes a lvalue argument
the rest passes a char const*

BENCHMARK                     CpuTime
-------------------------------------
// no alloc
bench_s_ref_short             2.53 ns 1c + 1c *
bench_s_copy_short            2.55 ns 1c + 1c
bench_s_copy_move_short       7.04 ns 1c + 1m    move costs more than mere copy if no alloc
bench_s_forward_short         7.01 ns 1c + 1m

// have alloc
bench_s_ref_long              37.7 ns 1c + 1c
bench_s_copy_long             38.1 ns 1c + 1c
bench_s_copy_move_long        19.0 ns 1c + 1m
bench_s_forward_long          18.9 ns 1c + 1m *

// no alloc
bench_ref_short               2.50 ns 1c + 1c
bench_copy_short              2.49 ns 1c + 1c
bench_copy_move_short         7.01 ns 1c + 1m
bench_forward_short           1.80 ns 1c      *

// have alloc
bench_ref_long                37.7 ns 1c + 1c
bench_copy_long               37.8 ns 1c + 1c
bench_copy_move_long          19.0 ns 1c + 1m
bench_forward_long            19.1 ns 1c      *

// no alloc
bench_l_ref_short             8.37 ns 1c
bench_l_copy_short            16.3 ns 1c + 1c
bench_l_copy_move_short       10.8 ns 1c + 1m
bench_l_forward_short         8.43 ns 1c      *

// have alloc
bench_l_ref_long              25.6 ns 1c
bench_l_copy_long             50.4 ns 1c + 1c
bench_l_copy_move_long        25.1 ns 1c + 1m
bench_l_forward_long          25.1 ns 1c      *
*/

#include <string>

#include <benchmark/benchmark.h>

using namespace std::string_literals;

static void bench_s_ref_short(benchmark::State& state) {
   
  struct foo {
   
    foo(std::string const& s) : s{
   s} {
   }
    std::string s;
  };
  for (auto _ : state) {
   
    foo f{
   "short"s};
    benchmark::DoNotOptimize(f);
    benchmark::ClobberMemory();
  }
}
BENCHMARK(bench_s_ref_short);

static void bench_s_copy_short(benchmark::State& state) {
   
  struct foo {
   
    foo(std::string s) : s{
   s} {
   }
    std::string s;
  };
  for (auto _ : state) {
   
    foo f{
   "short"s};
    benchmark::DoNotOptimize(&f);
    benchmark::ClobberMemory();
  }
}
BENCHMARK(bench_s_copy_short);

static void bench_s_copy_move_short(benchmark::State& state) {
   
  struct foo {
   
    foo(std::string s) : s{
   std::move(s)} {
   }
    std::string s;
  };
  for (auto _ : state) {
   
    foo f{
   "short"s};
    benchmark::DoNotOptimize(&f);
    benchmark::ClobberMemory();
  }
}
BENCHMARK(bench_s_copy_move_short);

struct fooT {
   
  template <typename T> fooT(T&& s) : s{
   std::forward<T>(s)} {
   }
  std::string s;
};
static void bench_s_forward_short(benchmark::State& state) {
   
  for (auto _ : state) {
   
    fooT f{
   "short"s};
    benchmark::DoNotOptimize(&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值