【发布时间】:2016-12-03 10:45:05
【问题描述】:
我开始使用 Gcov 来分析我的 C 程序。所以我在 GCov 文档的介绍中读到: “因为 gcov 按行(以最低分辨率)累积统计信息,所以它最适合在每行仅放置一个语句的编程风格。” GCov Documentation
在编写琐碎的程序时,我注意到计算包含在“for”子句中的语句执行次数的问题。
我将向您展示两个示例以及这些示例的 GCov 输出:
-: 0:Source:example1.c
-: 0:Graph:example1.gcno
-: 0:Data:example1.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:#include <stdlib.h>
-: 3:
-: 4:
1: 5:int main()
-: 6:{
1: 7: int i = 0;
-: 8:
1: 9: for(; i < 4; i++);
-: 10:
1: 11: return 0;
-: 12:}
-: 13:
GCov 将 for 子句视为唯一元素。是不是错了?正如文档所说,如果我将 for 子句中的语句放在一行中,GCov 会正确计算执行次数,如下例所示:
-: 0:Source:example1.c
-: 0:Graph:example1.gcno
-: 0:Data:example1.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:
-: 2:#include <stdio.h>
-: 3:#include <stdlib.h>
-: 4:
-: 5:
1: 6:int main()
-: 7:{
1: 8: int i = 0;
-: 9:
6: 10: for(;
-: 11: i < 4;
4: 12: i++);
-: 13:
1: 14: return 0;
-: 15:}
-: 16:
我们有 (i = 0) 被执行 1 次,(i
我有两个问题:
1-我不想调整我的编码风格来使用 GCov 进行统计。是否有许多工具或标志可以将我的代码转换为正确的格式,以便使用 GCov 获得正确的统计信息?
2- 是否有其他构造具有相同的“for”循环问题?
提前感谢您的回答。
【问题讨论】:
标签: c performance optimization gcov gcc4