C++中直接调用|函数指针|std::function|模板函数效率对比

C++中直接调用|函数指针|std::function|模板函数效率对比

0. 机器配置

阿里云1核2G

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                1
On-line CPU(s) list:   0
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 85
Model name:            Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
Stepping:              4
CPU MHz:               2500.010
BogoMIPS:              5000.02
Hypervisor vendor:     KVM
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              1024K
L3 cache:              33792K
NUMA node0 CPU(s):     0
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat spec_ctrl intel_stibp

1. 测试代码

/**
 * @file 
 * @brief 测试C++11 中 function,直接函数调用,函数指针调用与模板函数调用的效率
 * 
 */
#include <functional>
#include <iostream>
#include <typeinfo>
#include <chrono>

//待封装的函数
int Benchmark (int i, int j) {
    //int32_t index = rand() % 10000;
    int32_t index = 47;

    if (i < j)
        return (i*index -j) + 7 * j;
    else 
        return (i+j) * index;
}

typedef std::function<int (int, int) > FuncInt_IntInt;
using funcInt_IntInt = int (*) (int, int);  //函数指针

template<typename T, typename F>
int TBenchmark(T a, T b, const F& func)
{
    return func(a, b);
}

int main() {
    int a = 1;
    int b = a;
    int c = a;
    int d = a;
    int MAX_COUNTER = 1E9;
    FuncInt_IntInt fFunctional = Benchmark;
    funcInt_IntInt fFuncPtr = Benchmark;

    std::cout << "函数原型类型:\t\t" << typeid (Benchmark).name() << std::endl;
    std::cout << "std::funciton类型:\t" << typeid (fFunctional).name() << std::endl;
    std::cout << "函数指针类型:\t\t"  << typeid (fFuncPtr).name() << std::endl;


    std::chrono::time_point<std::chrono::system_clock> start, end;

    start = std::chrono::system_clock::now();
    for (size_t i = 0; i < MAX_COUNTER; i++) {
        a = Benchmark(a, 2);
    }
    end = std::chrono::system_clock::now();
    std::cout << "直接调用耗时:\t\t" << std::chrono::duration<double> (end - start).count() << "秒" << std::endl;

    start = std::chrono::system_clock::now();
    for (size_t i = 0; i < MAX_COUNTER; i++) {
        b = fFunctional (b, 2);
    }
    end = std::chrono::system_clock::now();
    std::cout << "使用std::function耗时:\t" << std::chrono::duration<double> (end - start).count() << "秒" << std::endl;

    start = std::chrono::system_clock::now();
    for (size_t i = 0; i < MAX_COUNTER; i++) {
        c = fFuncPtr (c, 2);
    }
    end = std::chrono::system_clock::now();
    std::cout << "使用函数指针耗时:\t" << std::chrono::duration<double> (end - start).count() << "秒" << std::endl;

    start = std::chrono::system_clock::now();
    for (size_t i = 0; i < MAX_COUNTER; i++) {
        d = TBenchmark(d, 2, Benchmark);
    }
    end = std::chrono::system_clock::now();
    std::cout << "使用模板函数耗时:\t" << std::chrono::duration<double> (end - start).count() << "秒" << std::endl;

    std::cout << "a=" << a << std::endl;
    std::cout << "b=" << b << std::endl;
    std::cout << "c=" << c << std::endl;
    std::cout << "d=" << c << std::endl;
    
    return 0;
}

2. 测试结果

# int32_t index = rand() % 10000 测试结果
函数原型类型:          FiiiE
std::funciton类型:     St8functionIFiiiEE
函数指针类型:          PFiiiE
直接调用耗时:          19.4755秒
使用std::function耗时: 44.6207秒
使用函数指针耗时:      17.8703秒
使用模板函数耗时:      20.2887秒
a=-575839072
b=-1680288092
c=884056696
d=884056696

# int32_t index = 47 测试结果
➜  test_home ./a.out
函数原型类型:          FiiiE
std::funciton类型:     St8functionIFiiiEE
函数指针类型:          PFiiiE
直接调用耗时:          10.7342秒
使用std::function耗时: 37.5376秒
使用函数指针耗时:      10.6478秒
使用模板函数耗时:      14.9356秒
a=234836599
b=234836599
c=234836599
d=234836599

3. 结论

调用速率:

直接调用 ≈ 函数指针 > 模板函数 > std::function

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值