c - 使用 GPROF 自动分析 C 程序

标签 c profiling gprof

我正在使用 gprof 分析矩阵乘法 C 程序。该 C 程序的一般结构如下;

int main()

{

int n;

printf("enter size of square matrices");
scanf("%d", &n);

data(matM); //fill matrices with n x n random data 
data(matN); 

// this is unoptimized algo
matUnopt(int *matM, int *matN, int *MatOut, int size);


// this is optimized algo
matOpt(int *matM, int *matN, int *MatOut, int size);

 return(0);

}   

现在我的分析方式是:

我运行我的可执行文件,将大小指定为100,然后
$ gprof mat gmon.out >analysis1.txt

这会生成analysis1.txt,我从中手动记录matOpt();matUnopt();的时间

然后我再次运行可执行文件 mat,并给出 n=150,然后

$  gprof mat gmon.out > analysis2.txt  

这会生成analysis2.txt,我从中手动记录matOpt();matUnopt();

的时间

等等。

这是非常耗时且无聊的方式。我想自动化这个过程。有些是这样的:

int main()

{

int n;


for (n=50; n<50000; n++)

{      
  data(matM); //fill matrices with n x n random data 
  data(matN); 

// this is unoptimized algo
matUnopt(int *matM, int *matN, int *MatOut, int size);


 //gprof records the time of completion of matUnopt for the particular value of n, and 
 puts in a file 

 // this is optimized algo
 matOpt(int *matM, int *matN, int *MatOut, int size);


 //gprof records the time of completion of matOpt for the particular value of n, and   
puts in a file 


  }

  return(0);

  }   

一旦应用程序退出,我期望一个具有如下表的文件:

run# (x 50)   profiling result (as usual we get from gprof)               

1             matUnopt  time .....
              matOpt    time .....  

2             matUnopt  time .....
              matOpt    time .....  

3             matUnopt  time .....
              matOpt    time .....  

4             matUnopt  time .....
              matOpt    time .....  


and so on.

请注意,上面的“分析结果”是我们通常从 gprof 获得的结果。 重要的是我有一种自动化的方法来获取可执行文件多次运行的函数的计时,也可以使用不同的输入大小。

这个解释只是一个粗略的想法。我很乐意得到任何与此类似的东西。例如,应用程序可能会退出,然后重新启动以获取新的分析结果。这就是我实际上正在做的事情。但我想自动执行此操作。

我如何实现这个目标?

最佳答案

您可以使用脚本语言并将您的大小作为分析二进制文件的参数吗?

在 c: passing arguments to main 中使用参数的示例.

这个 bash 脚本会自动运行您的程序,矩阵大小从 50 到 50000。tee 命令确保输出被打印并保存在相应的文件中:analysisxxx.txt

#!/bin/bash

for i in {50..50000}
do
    gprof mat gmon.out $i | tee analysis${i}.txt
done

希望这对你有一点帮助。

关于c - 使用 GPROF 自动分析 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20944097/

相关文章:

c++ - gprof - 文件缺少调用图数据

c - 设置 int 的前 10 位

c - 实现 Com 端口终端的最简单方法。 ( Windows ,C)

iphone - 关于用C制作游戏并将其跨平台传输(从iphone到android)的问题

performance - VS 2015 诊断工具窗口是空白的

python - Zipkin 用于分析传统程序的内部结构

c++ - 为什么循环摘要在 gprof 的调用图输出中没有任何调用者?

linux - 在 .so 库上使用 gprof?

c - 写入标准输入以进行自制管道

eclipse - 什么是分析,我如何分析我的 Java 程序?