C++函数对象与函数指针在sort上的性能测试

文章通过实验展示了在不同编译优化级别下,函数对象和函数指针在排序操作中的性能差异。初始测试结果显示函数指针更快,但随着编译优化的降低,函数对象在运行效率上展现出优势。实验表明,编译器优化可能影响两者的表现。
摘要由CSDN通过智能技术生成

最近在比较函数对象和函数指针的性能,看了一些文章,国内的如:
https://zhuanlan.zhihu.com/p/579128724

上面这篇文章是在GoogleTest当中进行测试的,其测试结果仅展示了一次,因此我认为不具备说服力,因为我在自己机子上也进行了四种测试,分别使用sort在模板函数对象、普通函数对象、模板函数指针和普通函数指针等四类问题上进行了性能测试。

起初,测试的多次结果普遍显示为函数指针要更快一些。说实话我还挺困惑的。。
在这里插入图片描述

然后看到stackoverflow里详细说明了函数对象是要比函数指针快的,特别是当我看到这样的回答时:

我想起来GCC在编译时可能对代码进行了优化处理导致我所展示的结果不够明确。所以我分别通过设置O0、O1、O2、O3优化来对比试验结果。stackoverflow上的问题见: 🔗 Link

我所做的实验结果展示为:
在这里插入图片描述
可以明显看到随着编译优化的降低,程序的运行效率上,函数对象更能够展现其优势。我的测试代码直接贴下面了,有需要的小伙伴自行运行吧。

#include <vector>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <algorithm>

const int MAX_NUMBER = 1000000;

template <typename T>
struct t_Greater_with_operator { bool operator()(T &a, T &b) { return a < b; } };

template <typename T>
bool t_Greater_with_function(T &a, T &b) { return a < b; }

struct Greater_with_operator { bool operator()(const int &a, const int &b) { return a < b; } };

bool Greater_with_function(const int &a, const int &b) { return a < b; }


int main() {
    std::vector<int> v1, v2, v3, v4;
    for(int i = MAX_NUMBER; i >= 0; --i) {
        v1.push_back(i);
        v2.push_back(i);
        v3.push_back(i);
        v4.push_back(i);
    }
    const auto start = std::chrono::high_resolution_clock::now();
    std::sort(v1.begin(), v1.end(), t_Greater_with_operator<int>());
    const auto end = std::chrono::high_resolution_clock::now();
    const std::chrono::duration<double, std::milli> elapsed = end - start;
    std::cout << "template function object: " << elapsed.count() << std::endl;

    const auto start_1 = std::chrono::high_resolution_clock::now();
    std::sort(v2.begin(), v2.end(), t_Greater_with_function<int>);
    const auto end_1 = std::chrono::high_resolution_clock::now();
    const std::chrono::duration<double, std::milli> elapsed_1 = end_1 - start_1;
    std::cout << "template function pointer: " << elapsed_1.count() << std::endl;

    const auto start_2 = std::chrono::high_resolution_clock::now();
    std::sort(v3.begin(), v3.end(), Greater_with_operator());
    const auto end_2 = std::chrono::high_resolution_clock::now();
    const std::chrono::duration<double, std::milli> elapsed_2 = end_2 - start_2;
    std::cout << "function object: " << elapsed_2.count() << std::endl;

    const auto start_3 = std::chrono::high_resolution_clock::now();
    std::sort(v4.begin(), v4.end(), Greater_with_function);
    const auto end_3 = std::chrono::high_resolution_clock::now();
    const std::chrono::duration<double, std::milli> elapsed_3 = end_3 - start_3;
    std::cout << "function pointer: " << elapsed_3.count() << std::endl;
    return 0;
}

关于函数对象和函数指针的使用场景,我直接贴出ChatGPT的回答,感觉有些道理,总结得很好:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值