C/C++语言性能分析方法及性能分析工具的使用_c语言性能分析-程序员宅基地

技术标签: C++  c++  C语言  c语言  常用工具  开发语言  

一、从算法复杂度都程序性能

我们第一次接触关于代码性能相关概念,应该是在学数据结构中的算法时间复杂度上面。

算法的时间复杂度反映了程序执行时间随输入规模增长而增长的量级,在很大程度上能很好反映出算法的优劣与否。因此,作为程序员,掌握基本的算法时间复杂度分析方法是很有必要的。
算法执行时间需通过依据该算法编制的程序在计算机上运行时所消耗的时间来度量。而度量一个程序的执行时间通常有两种方法。

一、事后统计的方法

这种方法可行,但不是一个好的方法。该方法有两个缺陷:一是要想对设计的算法的运行性能进行评测,必须先依据算法编制相应的程序并实际运行;二是所得时间的统计量依赖于计算机的硬件、软件等环境因素,有时容易掩盖算法本身的优势。

二、事前分析估算的方法

因事后统计方法更多的依赖于计算机的硬件、软件等环境因素,有时容易掩盖算法本身的优劣。因此人们常常采用事前分析估算的方法。

在编写程序前,依据统计方法对算法进行估算。一个用高级语言编写的程序在计算机上运行时所消耗的时间取决于下列因素:

(1). 算法采用的策略、方法;(2). 编译产生的代码质量;(3). 问题的输入规模;(4). 机器执行指令的速度。
一个算法是由控制结构(顺序、分支和循环3种)和原操作(指固有数据类型的操作)构成的,则算法时间取决于两者的综合效果。为了便于比较同一个问题的不同算法,通常的做法是,从算法中选取一种对于所研究的问题(或算法类型)来说是基本操作的原操作,以该基本操作的重复执行的次数作为算法的时间量度。

三、求解算法的时间复杂度的具体步骤

求解算法的时间复杂度的具体步骤是:

⑴ 找出算法中的基本语句;

算法中执行次数最多的那条语句就是基本语句,通常是最内层循环的循环体。

⑵ 计算基本语句的执行次数的数量级;

只需计算基本语句执行次数的数量级,这就意味着只要保证基本语句执行次数的函数中的最高次幂正确即可,可以忽略所有低次幂和最高次幂的系数。这样能够简化算法分析,并且使注意力集中在最重要的一点上:增长率。

⑶ 用大Ο记号表示算法的时间性能。

将基本语句执行次数的数量级放入大Ο记号中。

四、算法复杂度和程序性能之间的关系

首先从整体上来说,算法复杂度是针对代码片段的执行次数的一个量级估计的方法,只能从量级的角度评估该代码片段的性能。程序性能则是从实际运行的角度来衡量一个程序的性能。

算法复杂度只影响局部代码的性能,程序性能是对整个程序性能的评估。

算法复杂度是否会对整体程序性能有影响要视情况而定,还收到其他代码片段性能、执行次数、执行语句是否耗时等影响。

五、执行什么语句耗时?不同语句执行时间量级分析

之前写的一些博客可以拿来补充:

VxWorks实时性能探究

测试C语言中打印一句 hello world需要耗费多少时间

这次继续深入探究一下执行不同语句的耗时情况。

在我们的代码中,存在的比较典型的代码语句包括,算术运算,循环,逻辑判断,函数调用,打印,文件IO等。下面我们就测试一下这些语句在我电脑编译环境下的耗时情况。(注意不同的编译器和硬件配置会有不同的结果)

整型加和减:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    
	clock_t begin, end;
	double cost;
	//开始记录
	begin = clock();
	/*待测试程序段*/
	int a = 1;
	for (int i = 0; i < 100000000; i++) {
    
		a = a + 1;//a = a - 1;
	}

	//结束记录
	end = clock();
	cost = (double)(end - begin)/CLOCKS_PER_SEC;
	printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.055000 secs

55 ms/ 100000000 = 55000 us/100000000 = 0.00055 us = 0.55 ns

整型乘 和 整型加和减也差不多。

整型除 相比上面会更耗时,但量级差不多。

浮点型加和减
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    
	clock_t begin, end;
	double cost;
	//开始记录
	begin = clock();
	/*待测试程序段*/
	double a = 1.0;
	for (int i = 0; i < 100000000; i++) {
    
		a = a + 1;//a = a-1;
	}

	//结束记录
	end = clock();
	cost = (double)(end - begin)/CLOCKS_PER_SEC;
	printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.273000 secs

可以看出浮点型的加和减耗时大概是整型加减的5倍

浮点乘除:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    
	clock_t begin, end;
	double cost;
	//开始记录
	begin = clock();
	/*待测试程序段*/
	double a = 1.0;
	for (int i = 0; i < 100000000; i++) {
    
		a = a / i;
	}

	//结束记录
	end = clock();
	cost = (double)(end - begin)/CLOCKS_PER_SEC;
	printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.509000 secs

浮点型的乘和除耗时大概是浮点型的加和减耗时的2倍。

但总体来看进行算术运算的耗时还是比较小的。

测试打印printf
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    
	clock_t begin, end;
	double cost;
	//开始记录
	begin = clock();
	/*待测试程序段*/
	
	for (int i = 0; i < 1000; i++) {
    
		printf("h");
	}

	//结束记录
	end = clock();
	cost = (double)(end - begin)/CLOCKS_PER_SEC;
	printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.025000 secs

25 ms/ 1000 = 0.025 ms =25 us

测试还发现一个有趣的现象,打印语句耗时和打印的内容中字符的长短有关。

如果 printf(“h”) 改成 printf(“hh”) 、printf(“hhh”)、printf(“hhhh”)、printf(“hhhhh”)。则耗时分别变成

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.053000 secs

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.076000 secs

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.108000 secs

constant CLOCKS_PER_SEC is: 1000, time cost is: 0.142000 secs

差不多和字符的长度成正比。

函数调用

其实在C语言中,我们都会把经常要调用的代码不长的函数弄成宏或内联函数,这样可以提高运行效率。

这篇博客讲解了一下函数调用耗时的情况:函数调用太多了会有性能问题吗?

下面我们来测试一下函数调用:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int callme(int a) {
    
	a = a + 1;
	return a;
}

int main()
{
    
	clock_t begin, end;
	double cost;
	//开始记录
	begin = clock();
	/*待测试程序段*/
	int b;
	for (int i = 0; i < 1000000000; i++) {
    
		b = callme(i);
	}

	//结束记录
	end = clock();
	cost = (double)(end - begin)/CLOCKS_PER_SEC;
	printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}

constant CLOCKS_PER_SEC is: 1000, time cost is: 1.198000 secs

1.198s = 1198000000 ns / 1000000000 =1.19 ns

可以看到和上面那篇博客的计算的耗时是差不多的。

二、程序性能分析工具

1.gprof

gprof是一款 GNU profile工具,可以运行于linux、AIX、Sun等操作系统进行C、C++、Pascal、Fortran程序的性能分析,用于程序的性能优化以及程序瓶颈问题的查找和解决。

gprof介绍

gprof(GNU profiler)是GNU binutils工具集中的一个工具,linux系统当中会自带这个工具。它可以分析程序的性能,能给出函数调用时间、调用次数和调用关系,找出程序的瓶颈所在。在编译和链接选项中都加入-pg之后,gcc会在每个函数中插入代码片段,用于记录函数间的调用关系和调用次数,并采集函数的调用时间。

gprof安装

gprof是gcc自带的工具,一般无需额外安装步骤。

首先检查工具是否已经安装在系统上。 为此,只需在终端中运行以下命令即可。

$ gprof

如果您收到以下错误:

$ a.out: No such file or directory

那么这意味着该工具已经安装。 否则可以使用以下命令安装它:

$ apt-get install binutils

gprof使用步骤

1. 用gcc、g++、xlC编译程序时,使用-pg参数

如:g++ -pg -o test.exe test.cpp

编译器会自动在目标代码中插入用于性能测试的代码片断,这些代码在程序运行时采集并记录函数的调用关系和调用次数,并记录函数自身执行时间和被调用函数的执行时间。

2. 执行编译后的可执行程序,生成文件gmon.out

如:./test.exe

该步骤运行程序的时间会稍慢于正常编译的可执行程序的运行时间。程序运行结束后,会在程序所在路径下生成一个缺省文件名为gmon.out的文件,这个文件就是记录程序运行的性能、调用关系、调用次数等信息的数据文件。

3. 使用gprof命令来分析记录程序运行信息的gmon.out文件

如:gprof test.exe gmon.out

可以在显示器上看到函数调用相关的统计、分析信息。上述信息也可以采用gprof test.exe gmon.out> gprofresult.txt重定向到文本文件以便于后续分析。

实战一:用gprof测试基本函数调用及控制流
测试代码
#include <stdio.h>

void loop(int n){
    
    int m = 0;
    for(int i=0; i<n; i++){
    
        for(int j=0; j<n; j++){
    
            m++;    
        }   
    }
}

void fun2(){
    
    return;
}

void fun1(){
    
    fun2();
}

int main(){
    
    loop(10000);

    //fun1callfun2
    fun1(); 

    return 0; 
}
操作步骤
liboxuan@ubuntu:~/Desktop$ vim test.c
liboxuan@ubuntu:~/Desktop$ gcc -pg -o test_gprof test.c 
liboxuan@ubuntu:~/Desktop$ ./test_gprof 
liboxuan@ubuntu:~/Desktop$ gprof ./test_gprof gmon.out
# 报告逻辑是数据表 + 表项解释
Flat profile:

# 1.第一张表是各个函数的执行和性能报告。
Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
101.20      0.12     0.12        1   121.45   121.45  loop
  0.00      0.12     0.00        1     0.00     0.00  fun1
  0.00      0.12     0.00        1     0.00     0.00  fun2

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.


Copyright (C) 2012-2015 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.


# 2.第二张表是程序运行时的
             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 8.23% of 0.12 seconds

index % time    self  children    called     name
                0.12    0.00       1/1           main [2]
[1]    100.0    0.12    0.00       1         loop [1]
-----------------------------------------------
                                                 <spontaneous>
[2]    100.0    0.00    0.12                 main [2]
                0.12    0.00       1/1           loop [1]
                0.00    0.00       1/1           fun1 [3]
-----------------------------------------------
                0.00    0.00       1/1           main [2]
[3]      0.0    0.00    0.00       1         fun1 [3]
                0.00    0.00       1/1           fun2 [4]
-----------------------------------------------
                0.00    0.00       1/1           fun1 [3]
[4]      0.0    0.00    0.00       1         fun2 [4]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Copyright (C) 2012-2015 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
# 第三张表是函数与其在报告中序号的对应表

Index by function name

   [3] fun1                    [4] fun2                    [1] loop
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41854911/article/details/122245100

智能推荐

zookeeper——监控命令详解_zookeeper监控-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏2次。前言zookeeper的监控命令需要通过telnet或者nc工具向zookeeper服务进行提交如使用telnet工具:telnet 127.0.0.1 2181之后telnet工具连接zookeeper成功可以使用四字监控命令进行操作。在连接建立之后输入对应的命令后回车。在使用监控命令之前,需要修改zookeeper的配置文件,开启四字监控命令,否则会报错如下:nc命令使用方法自行搜索,使用什么工具与本文关系不大。开启监控命令修改zoo.cfg文件,在配置文件中加入如_zookeeper监控

Spring IOC常见的使用方式-程序员宅基地

文章浏览阅读877次,点赞15次,收藏19次。IoC (Inversion of Control) : 控制反转, 是一个理论,概念,思想。把对象的创建,赋值,管理工作都交给代码之外的容器实现, 也就是对象的创建是有其它外部资源完成,这样做实现了与解耦合。正转:对象的创建、赋值等操作交由程序员手动完成,即使用类似new Xxx(Xxx Xxx)、Xxx.setXxx()语句完成对象的创建与赋值,缺点是一旦程序功能发生改变,涉及到的类就要修改代理,耦合度高,不便于维护和管理。

jquery、js获取table,遍历输出tr中各个td的内容_js如何获取tr中的某个td的值-程序员宅基地

文章浏览阅读1.4k次。使用jquery获取:js代码:$('#btntb').click(function(){ $('#tab tr').each(function(i){ // 遍历 tr $(this).children('td').each(function(j){ // 遍历 tr 的各个 td alert("第"+(i+1)+"行,第"+(j+1)+"个td的值:"+$(this).text()+"。"); }); });_js如何获取tr中的某个td的值

敏捷测试:探索灵活高效的软件测试方法_敏捷开发环境中,软件测试如何适应和变化-程序员宅基地

文章浏览阅读118次。通过尽早开始测试、持续集成和自动化、小步快跑、频繁交付和协作与沟通等原则,团队可以提高测试效率和质量,及时交付可用的软件版本。通过遵循敏捷测试的原则和实践,团队可以提高测试效率和质量,及时交付可用的软件版本。缺乏足够的时间和资源:由于敏捷开发的迭代周期较短,测试人员可能面临缺乏足够的时间和资源的问题。敏捷测试的概念:敏捷测试是一种基于敏捷软件开发理念的测试方法,强调快速响应变化、持续交付价值和紧密协作。因此,敏捷测试需要采用灵活的需求管理方法,如故事地图、用户故事等,以便更好地理解和管理需求。_敏捷开发环境中,软件测试如何适应和变化

STM32实现呼吸灯和流水灯_stm32103r6做呼吸灯仿真实验代码-程序员宅基地

文章浏览阅读5.3k次,点赞3次,收藏35次。单片机 呼吸灯 流水灯_stm32103r6做呼吸灯仿真实验代码

php支付宝接口实例文档,支付宝接口实例php版-程序员宅基地

文章浏览阅读179次。支付页面:$payr['paymethod']=2;if($payr['paymethod']==0)//双接口{$use_service='trade_create_by_buyer';}elseif($payr['paymethod']==2)//担保接口{$use_service='create_partner_trade_by_buyer';}else//即时到帐接口{$use_servi..._payment_type=1&out_trade_no=ncre_51-2024010312-8051852778∂ner=208822

随便推点

计算机网络到底讲了些什么_计算机网络到底在讲什么-程序员宅基地

文章浏览阅读6.4k次,点赞46次,收藏136次。小智:大鹏哥,我最近看了下计算机网络,把书上的内容都过了一遍,可是感觉还是串不起来,不知道计算机网络到底讲了些什么内容,也不知道重点在哪里,你能不能给我梳理梳理呀!计算机为什么要联网大鹏:行,那咱们就从“计算机网络”这五个字说起,计算机网络,顾名思义,就是由计算机组成的网络,那计算机组成网络要干什么呢?我们知道,不联网的计算机只能单兵作战,只能玩单机游戏,只能简单的处理文档,而通过联网,计算机可以玩网游,可以看电影,可以聊QQ,总之,计算机联网之后功能大大扩展。那回到刚刚计算机要组成网络的问题,很显_计算机网络到底在讲什么

ARCore教程 - 构建AR元宇宙博物馆应用_unity arfoundation实现ar元宇宙-程序员宅基地

文章浏览阅读209次。通过深入了解ARCore和Unity的功能和工具,你可以进一步扩展和改进你的AR应用程序,并为用户带来更加丰富和令人印象深刻的体验。我们将学习如何将现实世界中的虚拟博物馆展品与用户的环境相结合,使用户能够通过移动设备的摄像头在其周围浏览并与展品进行交互。这将在场景中添加一个AR摄像头,用于捕捉用户的环境。根据你选择的目标平台,选择正确的构建设置,并将应用程序安装在支持ARCore的Android或iOS设备上。你可以在Unity的场景视图中调整展品的位置、旋转和缩放,以便它在用户的环境中看起来合适。_unity arfoundation实现ar元宇宙

基于ssm高校工会提案管理信息系统的设计与开发的设计与实现(源码+lw+部署文档+讲解等)-程序员宅基地

文章浏览阅读583次,点赞17次,收藏21次。功能对照表的目的是帮助开发团队了解软件的功能状况,及时修复功能缺陷和错误,并提高软件的质量和稳定性。功能编号功能名称功能描述功能状态备注1用户登录用户可以通过提供用户名和密码登录系统正常用户名和密码的验证机制安全性2用户注册用户可以通过提供用户名、密码和电子邮件地址注册新的账户正常无3密码修改用户可以通过提供原密码和新密码修改已有账户的密码正常用户密码的修改操作是否需要提供安全认证4用户信息查看用户可以查看自己的个人信息,如用户名、电子邮件地址、角色等正常无。

《奔跑吧Linux内核》之处理器体系结构_奔跑吧linux内核(第2版)卷1:基础架构-程序员宅基地

文章浏览阅读3.1k次。本文摘自人民邮电出版社异步社区《奔跑吧Linux内核》 第1章 处理器体系结构京东购书:https://item.jd.com/12152745.html 试读地址:http://www.epubit.com.cn/book/details/4835本章思考题 1.请简述精简指令集RISC和复杂指令集CISC的区别。 2.请简述数值0x12345678在大小端字节序处理器的存储器中的存储方_奔跑吧linux内核(第2版)卷1:基础架构

【路径规划】基于matlab吉萨金子塔建造算法栅格地图机器人路径规划【含Matlab源码 2835期】_matlab栅格图路径规划-程序员宅基地

文章浏览阅读582次。吉萨金子塔建造算法栅格地图机器人路径规划完整的代码,方可运行;可提供运行操作视频!适合小白!_matlab栅格图路径规划

推荐文章

热门文章

相关标签