gpt4 book ai didi

C++ 性能、优化编译器、.cpp 中的空函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:05 25 4
gpt4 key购买 nike

我有一个非常基本的类,将其命名为 Basic,用于一个更大项目中的几乎所有其他文件。在某些情况下,需要有调试输出,但在 Release模式下,这不应该启用并且是一个 NOOP。

目前在标题中有一个定义,它根据设置打开或关闭宏。因此,当关闭时,这绝对是一个 NOOP。我想知道,如果我有以下代码,编译器 (MSVS/gcc) 是否能够优化函数调用,那么它又是一个 NOOP。 (通过这样做,开关可以在 .cpp 中,并且开关会更快,编译/链接时间明智)。

--Header--
void printDebug(const Basic* p);

class Basic {
Basic() {
simpleSetupCode;

// this should be a NOOP in release,
// but constructor could be inlined
printDebug(this);
}
};
--Source--
// PRINT_DEBUG defined somewhere else or here
#if PRINT_DEBUG
void printDebug(const Basic* p) {
// Lengthy debug print
}
#else
void printDebug(const Basic* p) {}
#endif

最佳答案

与所有此类问题一样,答案是 - 如果它对您来说真的很重要,请尝试该方法并检查发出的汇编语言。

关于C++ 性能、优化编译器、.cpp 中的空函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com