未使用的代码中的开销 [英] Overhead in unused code

查看:175
本文介绍了未使用的代码中的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你的代码中有没有使用的函数的开销。

I am wondering what the overhead is of having unused functions in your code.

例如,你有一些调试日志,然后给你的大多数对象在调试日志中使用的ToString()函数。

Say for example you have some debug logging, and you then give most of your objects a ToString() function that is being used in the debug logs.

在未使用调试日志记录的版本构建中。那么它是否值得删除那些ToString()函数的源代码? (例如通过宏?)

In a release build that debug logging is not being used. Is it then worth it removing the source code of those ToString() functions? (e.g. via Macro?)

或者,它们只是使可执行文件稍大一点,否则不会影响性能?例如没有速度影响?或者编译器或链接器可能甚至删除函数,如果他们不使用?如果编译器或链接器不删除代码,如果ToString()函数是内联定义的?假设它会尝试内联代码,并且由于函数从未被调用,它会消失?

Or do they just make the executable marginally larger and otherwise don't impact performance? e.g. no speed impact? Or does the compiler or linker possibly even remove the functions if they are not used? If the compiler or linker don't remove the code, what if the ToString() functions were defined inline? Presumably it would try to inline the code, and since the function is never called, it would disappear?

我想每个函数都需要保留在静态库中,但是一旦编译为可执行文件,肯定很多东西只被链接器忽略了。

I imagine every function needs to be retained in a static lib, but once compiled to an executable, surely a lot of stuff just gets ignored by the linker?

另一个注意是大致相似,如果编译器选择不内联一个内联函数,以便内联函数在几个编译单元中被定义为函数,链接器会删除多余的定义,并且只在最后链接其中一个?

On another note that is roughly similar, if the compiler chooses not to inline an inline function, so that the inline function is defined as function in several compilation units, will the linker chuck away the superfluous definitions and only link one of them at the end?

谢谢

推荐答案

这取决于编译器和我想的优化级别。

It depends on the compiler and, I guess, optimization level.

G ++和MSVC ++删除未使用的内联函数,但保留未使用的非内联函数。
例如,在正常程序中只使用STL的一小部分。

G++ and MSVC++ remove unused inline functions but keep unused non-inline functions. For instance, you use only a small fraction of the STL in a normal program. All unused functions get removed, because they're defined as inline.

另一方面,GCC保留所有函数,甚至未使用的内联函数。

GCC on the other hand keeps all functions, even unused inline ones.

回答你的另一个问题:如果一个函数以某种方式在多个编译单元中定义,链接器将皱眉并拒绝链接,除非它被定义为内联。

Answer to your other question: if a function is somehow defined in multiple compilation units, the linker will frown and refuse to link, unless if it is defined as inline.

这篇关于未使用的代码中的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆