基于perf的C++中的inline与函数对象性能差异分析

本文通过对比内联函数与Functor在std::sort中的应用,发现Functor方式性能更优。实验中,两者分别用于比较16M个随机整数的排序时间,并利用perf工具进行了详细的性能分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

相关命令和工具

(1)nm a.out|grep compare

(2)g++ -O2 -Winline main.cpp

Winline对含有inline关键字的而没有进行inline进行警告

(3)perf

 

  • 统计基准程序:

(1)通过随机生成16M个整数,进行排序,统计排序所需要要的时间,

(2)基于Linux平台,GCC,参数选项O2级优化

g++ -O2 main.cpp

程序如下:

#include <stdlib.h>
#include <vector>
#include <sys/stat.h>
#include <sys/timeb.h>
inline bool compare(int a, int b)
{
    return a < b;
}

struct Functor {
    bool operator()(int a, int b) const {
        return a < b;
    }
};
int main(int argc,char*argv[])
{
    std::vector<int> vec1;
    std::vector<int> vec2;
    size_t count = 16*1024*1024;
    vec1.reserve(count);
    vec2.reserve(count);
    srand(0XDeadBeaf);


    long long t1=0;
    long long t2=0;
    //for(int j=0;j<16;j++)
    {
        vec1.clear();
        vec2.clear();
        for(size_t i=0;i<count;i++)
        {
            int rd = rand();
            vec1.push_back(rd);
            vec2.push_back(rd);
        }
        clock_t t2_start=clock();
        std::sort(vec2.begin(), vec2.end(), Functor());
        clock_t t2_end = clock();

        clock_t t1_start=clock();
        std::sort(vec1.begin(), vec1.end(), compare);
        clock_t t1_end=clock();
        t1 += t1_end-t1_start;
        t2 += t2_end-t2_start;
    }
    printf("1 cost:%d\n2 cost:%d\n",(t1)/(CLOCKS_PER_SEC/1000),(t2)/(CLOCKS_PER_SEC/1000));
    getchar();
    return 0;
}

 

 

  • 统计结果

1 cost:4090

2 cost:3030

 

1为使用内联函数方式,2为Functor方式

 

  • 基于perf程序性能分析

统计使用方式如下:

(1)统计性能到文件中(其余参数,man perf,这里都忽略)

$>perf record ./a.out

(2)显示分析报告

$>perf report

 

基于该统计结果进行分析如下:

 

  • 使用函数compare方式
   0.65 :          401209:       callq  *%rbx                                                                                                                             a
    4.03 :          40120b:       test   %al,%al                                                                                                                           a
    0.00 :          40120d:       lea    0x4(%r12),%rdx                                                                                                                    a
    0.00 :          401212:       jne    401200 <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, bool (*)(int`
    7.04 :          401214:       mov    0x8(%rsp),%r15                                                                                                                    a
    7.36 :          401219:       sub    $0x4,%r15                                                                                                                         a
    0.71 :          40121d:       nopl   (%rax)                                                                                                                            a
    9.30 :          401220:       mov    %r15,0x8(%rsp)                                                                                                                    a
   14.59 :          401225:       mov    (%r15),%esi                                                                                                                       a
    3.03 :          401228:       sub    $0x4,%r15                                                                                                                         a
    0.00 :          40122c:       mov    0x4(%rsp),%edi                                                                                                                    a
    1.97 :          401230:       callq  *%rbx                                                                                                                             a
    5.10 :          401232:       test   %al,%al                                                                                                                           a
    0.00 :          401234:       jne    401220 <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, bool (*)(inta
    7.88 :          401236:       cmp    0x8(%rsp),%r12                                                                                                                    a
    0.00 :          40123b:       jae    401255 <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, bool (*)(inta
    9.52 :          40123d:       mov    0x8(%rsp),%rcx   

 

  • Functor方式
   11.71 :          400e50:       add    $0x4,%r13                                                                                                                         a
    4.69 :          400e54:       mov    0x0(%r13),%edx                                                                                                                    a
    8.13 :          400e58:       cmp    %edx,%edi                                                                                                                         a
    0.00 :          400e5a:       jg     400e50 <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, Functor>(__ga
    9.77 :          400e5c:       cmp    %ecx,%edi                                                                                                                         a
    5.07 :          400e5e:       lea    -0x4(%rax),%r8                                                                                                                    `
    0.92 :          400e62:       jge    400e7d <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, Functor>(__ga
    4.31 :          400e64:       sub    $0x8,%rax                                                                                                                         a
    1.95 :          400e68:       nopl   0x0(%rax,%rax,1)                                                                                                                  a
    9.50 :          400e70:       mov    (%rax),%ecx                                                                                                                       a
   10.15 :          400e72:       mov    %rax,%r8                                                                                                                          a
    0.04 :          400e75:       sub    $0x4,%rax                                                                                                                         a
    0.08 :          400e79:       cmp    %ecx,%edi                                                                                                                         a
    0.00 :          400e7b:       jl     400e70 <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, Functor>(__ga
   11.06 :          400e7d:       cmp    %r13,%r8                                                                                                                          a
    5.04 :          400e80:       jbe    400e9e <void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, Functor>(__ga
    4.01 :          400e82:       mov    %ecx,0x0(%r13)                                                                                                                    a
    6.98 :          400e86:       add    $0x4,%r13                                                                                                                         a
    0.04 :          400e8a:       mov    %edx,(%r8)                                                                                                                        a
    3.01 :          400e8d:       mov    -0x4(%r8),%ecx                                                                                                                    a
    0.57 :          400e91:       mov    0x0(%r13),%edx 

 

 

  • 总耗时概览
 41.09%  a.out  a.out              [.] void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, bool (*)(int, int)>(__`
 34.57%  a.out  a.out              [.] void std::__introsort_loop<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, long, Functor>(__gnu_cxx::__a
 12.15%  a.out  a.out              [.] main                                                                                                                                a
  7.64%  a.out  a.out              [.] compare(int, int)  

 

对比1,2,红色部分;可以看出,在std:sort中,compare并不能被内联,虽然已经声明为内联。

callq %rbx即为调用compare函数,因此,造成了性能影响。

 

  • 结论:

(1)Functor函数对象比内联函数,在std::sort这种复杂的函数中(存在递归,循环),效率更高。

(2)内联,由编译器决定,通常不影响性能

 

 

 

 

 

google-perftools 简介 google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。 1. 编译目标程序,加入对 google-perftools 库的依赖。 2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。 3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。 安装 您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。为完成步骤 3 的工作,您还需要一个将剖析结果转化为程序员可读文档的工具,例如 gv(http://www.gnu.org/software/gv/)。 编译运行 您需要在原有的编译选项中加入对 libprofiler.so 的引用,这样在目标程序运行时会加载工具的动态库。例如本例中作者的系统中,libprofiler.so 安装在"/usr/lib"目录下,所以需要在 makefile 文件中的编译选项加入“-L/usr/lib -lprofiler”。 google-perftools 需要在目标代码的开始和结尾点分别调用剖析模块的启动和终止函数,这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析。工具的启动和终止可以采用以下两种方式。 a. 使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数。 gdb 是 Linux 上广泛使用的调试工具,它提供了强大的命令行功能,使我们可以在程序运行时插入断点并在断点处执行其他函数。具体的文档请参照 http://www.gnu.org/software/gdb/,本文中将只对用到的几个基本功能进行简单介绍。使用以下几个功能就可以满足我们性能调试的基本需求,具体使用请参见下文示例。 命令 功能 ctrl+c 暂停程序的运行 c 继续程序的运行 b 添加函数断点(参数可以是源代码中的行号或者一个函数名) p 打印某个量的值或者执行一个函数调用 b. 在目标代码中直接加入性能工具函数的调用,该方法就是在程序代码中直接加入调试函数的调用。 两种方式都需要对目标程序重新编译,加入对性能工具的库依赖。对于前者,他的好处是使用比较灵活,但工具的启动和终止依赖于程序员的手动操作,常常需要一些暂停函数(比如休眠 sleep)的支持才能达到控制程序的目的,因此精度可能受到影响。对于后者,它需要对目标代码的进行修改,需要处理函数声明等问题,但得到的结果精度较高,缺点是每次重新设置启动点都需要重新编译,灵活度不高,读者可以根据自己的实际需求采用有效的方式。 示例详解 该程序是一个简单的例子,文中有两处耗时的无用操作,并且二者间有一定的调用关系。 清单 1. 示例程序 void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; void main(){ int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); //ProfilerStart("CPUProfile"); while( i++ MyProfile.pdf 转换后产生的结果文档如下图。图中的数字和框体的大小代表了的某个函数的运行时间占整个剖析时间的比例。由代码的逻辑可知,stupidComputing,stupidComputing2 都是费时操作并且它们和 consumeSomeCPUTime 存在着一定的调用关系。 图 1. 剖析结果 结束语 本文介绍了一个 Linux 平台上的性能剖析工具 google-perftools,并结合实例向读者展示了如何使用该工具配置、使用及分析性能瓶颈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值