2024年Go最全C C++语言性能分析方法及性能分析工具的使用,Golang开发核心知识笔记共2100页

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

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

在我们的代码中,存在的比较典型的代码语句包括,算术运算,循环,逻辑判断,函数调用,打印,文件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


![img](https://img-blog.csdnimg.cn/img_convert/2afd07d9a4923e625e3e7644fcbdbfa4.png)
![img](https://img-blog.csdnimg.cn/img_convert/a4d6ed32e1e0c265d70446dd81036e3d.png)
![img](https://img-blog.csdnimg.cn/img_convert/2774c438ea7db22067623fa53f4bd0ad.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

all    function and its descendents per call, if this


[外链图片转存中...(img-6BFiJDX8-1715366273445)]
[外链图片转存中...(img-N1InmlKM-1715366273445)]
[外链图片转存中...(img-mCNahHWD-1715366273445)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值