Is there a method/function to get the code size of a C program compiled using GCC compiler? (may vary when some optimization technique is applied)

老子叫甜甜 提交于 2021-01-29 08:14:51

问题


  • Can I measure the code size with the help of an fseek() function and store it to a shell variable?
  • Is it possible to extract the code size, compilation time and execution time using milepost gcc or a GNU Profiler tool? If yes, how to store them into shell variables?

Since my aim is to find the best set of optimization technique upon the basis of the compilation time, execution time and code size, I will be expecting some function that can return these parameters.

MyPgm=/root/Project/Programs/test.c
gcc -Wall -o1 -fauto-inc-dec $MyPgm -o output
time -f "%e" -o Output.log ./output
while read line;
do
echo -e "$line";
Val=$line
done<Output.log

This will store the execution time to the variable Val. Similarly, I want to get the values of code size as well as compilation time. I will prefer something that I can do to accomplish this, without using an external program!


回答1:


for code size on linux, you can use size command on terminal. $size file-name.out

it will give size of different sections. use text section for code size. you can use data and bss if you want to consider global data size as well.




回答2:


You can use the size(1) command http://www.linuxmanpages.com/man1/size.1.php

Or open the ELF file, walk over section headers and sum the sizes of all the section with type SHT_PROGBITS and the SHF_EXECINSTR flag set.




回答3:


On non-Linux / non-GNU-utils systems (where you may have neither GNU size nor readelf), the nm program can be used to dump symbol information (including sizes) from object files (libraries / executables). The syntax is slightly system-dependent:

  • OpenGroup manpage for nm (the "portable subset")
  • Linux/BSD manpage for nm (GNU version)
  • Solaris manpage for nm
  • AIX manpage for nm
  • nm usage on HP/UX (this says "PA-RISC" but the utility is present / usable on Itanium)
  • Windows: Doesn't have nm as such, but see: Microsoft equivalent of the nm command

Unfortunately, while the utility is available almost everywhere, its output format is not as portable as could be, so some system-specific scripting is necessary.



来源:https://stackoverflow.com/questions/13487113/is-there-a-method-function-to-get-the-code-size-of-a-c-program-compiled-using-gc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!