关于 gcc的编译选项 -O -O2 以及 gprof的一个问题

时间:2022-12-27 19:59:49
我想做一个性能测试

在不使用 -O或者-O2 编译选项的时候
gprof 结果中有我想要测试的函数信息,如下:

========================================
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 99.99     94.08    94.08    60000     1.57     1.57  acsmSearchSparseDFA_Full
  0.01     94.09     0.01                             main
  0.00     94.09     0.00      256     0.00     0.00  List_GetNextState
  0.00     94.09     0.00      256     0.00     0.00  List_GetNextState2
  0.00     94.09     0.00      206     0.00     0.00  AC_MALLOC
  0.00     94.09     0.00       53     0.00     0.00  AC_FREE
  0.00     94.09     0.00       50     0.00     0.00  AddMatchListEntry
  0.00     94.09     0.00       50     0.00     0.00  AddPatternStates
  0.00     94.09     0.00       50     0.00     0.00  ConvertCaseEx
  0.00     94.09     0.00       50     0.00     0.00  acsmAddPattern2
  0.00     94.09     0.00        4     0.00     0.00  queue_count
  0.00     94.09     0.00        2     0.00     0.00  queue_free
  0.00     94.09     0.00        2     0.00     0.00  queue_init
  0.00     94.09     0.00        1     0.00     0.00  Build_NFA
  0.00     94.09     0.00        1     0.00     0.00  Conv_List_To_Full
  0.00     94.09     0.00        1     0.00     0.00  Convert_NFA_To_DFA
  0.00     94.09     0.00        1     0.00     0.00  List_ConvToFull
  0.00     94.09     0.00        1     0.00     0.00  List_FreeTransTable
  0.00     94.09     0.00        1     0.00     0.00  Print_DFA
  0.00     94.09     0.00        1     0.00     0.00  acsmCompile2
  0.00     94.09     0.00        1     0.00     0.00  acsmFree2
  0.00     94.09     0.00        1     0.00     0.00  acsmNew2
  0.00     94.09     0.00        1     0.00     0.00  acsmPrintInfo2
  0.00     94.09     0.00        1     0.00     0.00  acsmUpdateMatchStates
  0.00     94.09     0.00        1     0.00     0.00  init_xlatcase

=========================================

就是函数 acsmSearchSparseDFA_Full

但是加上 -O或者-O2以后,结果就分别都不见了我想观察的函数,如下(其它函数项都完全一样):
=========================================
lat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
100.00     10.86    10.86                             main
  0.00     10.86     0.00      256     0.00     0.00  List_GetNextState
  0.00     10.86     0.00      256     0.00     0.00  List_GetNextState2
  0.00     10.86     0.00      206     0.00     0.00  AC_MALLOC
  0.00     10.86     0.00       53     0.00     0.00  AC_FREE
  0.00     10.86     0.00       50     0.00     0.00  AddMatchListEntry
  0.00     10.86     0.00       50     0.00     0.00  AddPatternStates
  0.00     10.86     0.00       50     0.00     0.00  acsmAddPattern2
  0.00     10.86     0.00        4     0.00     0.00  queue_count
  0.00     10.86     0.00        2     0.00     0.00  queue_free
  0.00     10.86     0.00        2     0.00     0.00  queue_init
  0.00     10.86     0.00        1     0.00     0.00  Build_NFA
  0.00     10.86     0.00        1     0.00     0.00  Conv_List_To_Full
  0.00     10.86     0.00        1     0.00     0.00  Convert_NFA_To_DFA
  0.00     10.86     0.00        1     0.00     0.00  List_ConvToFull
  0.00     10.86     0.00        1     0.00     0.00  List_FreeTransTable
  0.00     10.86     0.00        1     0.00     0.00  Print_DFA
  0.00     10.86     0.00        1     0.00     0.00  acsmCompile2
  0.00     10.86     0.00        1     0.00     0.00  acsmFree2
  0.00     10.86     0.00        1     0.00     0.00  acsmNew2
  0.00     10.86     0.00        1     0.00     0.00  acsmPrintInfo2
  0.00     10.86     0.00        1     0.00     0.00  acsmUpdateMatchStates
  0.00     10.86     0.00        1     0.00     0.00  init_xlatcase
=========================================

这是什么原因呢 ???

8 个解决方案

#1


o2优化在某些gcc中是有问题吧.

#2


gcc可是多少人在使用的,不大可能栽在这个小程序上的吧。。。

何况增加了 O2、O选项,运行结果也都是没有问题的啊

总部不会是gcc加了个选项就 优化掉了一个函数吧?!

#3


谁知道呢。。。也许把这个函数扩展成inline了。。。

#4


没人了解么???

#5


o2之后是可能被优化掉了,或者展开了。

#6


进行了优化

#7


扩展成inline是-O3 才做的优化.

还要再找答案

#8


原先的:
 % cumulative self self total
time seconds seconds calls ms/call ms/call name
99.99 94.08 94.08 60000 1.57 1.57 acsmSearchSparseDFA_Full
0.01 94.09 0.01 main

后来的:
 % cumulative self self total
time seconds seconds calls Ts/call Ts/call name
100.00 10.86 10.86 main

说明在main里面花费了大量时间, 原先则是earchSparseDFA_Full耗了大量时间.
你反汇编可执行文件看看, acsmSearchSparseDFA_Full的代码可能被放到main中了.

#1


o2优化在某些gcc中是有问题吧.

#2


gcc可是多少人在使用的,不大可能栽在这个小程序上的吧。。。

何况增加了 O2、O选项,运行结果也都是没有问题的啊

总部不会是gcc加了个选项就 优化掉了一个函数吧?!

#3


谁知道呢。。。也许把这个函数扩展成inline了。。。

#4


没人了解么???

#5


o2之后是可能被优化掉了,或者展开了。

#6


进行了优化

#7


扩展成inline是-O3 才做的优化.

还要再找答案

#8


原先的:
 % cumulative self self total
time seconds seconds calls ms/call ms/call name
99.99 94.08 94.08 60000 1.57 1.57 acsmSearchSparseDFA_Full
0.01 94.09 0.01 main

后来的:
 % cumulative self self total
time seconds seconds calls Ts/call Ts/call name
100.00 10.86 10.86 main

说明在main里面花费了大量时间, 原先则是earchSparseDFA_Full耗了大量时间.
你反汇编可执行文件看看, acsmSearchSparseDFA_Full的代码可能被放到main中了.