c ++试图通过替换测试来优化代码 [英] c++ attempt to optimize code by replacing tests

查看:109
本文介绍了c ++试图通过替换测试来优化代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看别人写的代码,它有很多调试部分,类型为

  if 0){code} 

if(1){code} code>



if(false){code}



还有

  #if(0)
#endif



(虽然没有变成灰色 - 我认为它应该)



我想知道,如果我用一些 #if 0 (或 #ifdef _DEBUG )替换它,是它可能优化代码? - 或 - 它不会有什么区别?



我认为它可能有帮助,因为我看到在这些部分的代码是灰色的 - 我想这个代码从Release可执行文件中删除...因此使它更快。是真的吗?



我想的代码是可以被调用很多次的函数...



编辑:我指的代码正在运行数百万次。我知道if(0)的内容将被忽略...



我也知道能够轻松调试问题的好处,将测试从0切换到1 ...



我的问题是,我添加了上百万次的测试if(0)不添加开销...我想弄明白什么是所有的东西,可以使这段代码需要更少的时间。

解决方案

如果放置在这些IF中的表达式在编译时常量且可确定可能几乎肯定编译器已经为你删除了它们的代码。



当然,如果你在Debug-Mode下编译,和/或如果你有优化-level设置为零,那么编译器可以跳过那些测试 - 但是使用纯零/一/真/假值是很不可能的。



编译时常数分支,你可以确定编译器删除了死的。



它甚至可以删除复杂的情况,如:

  const int x = 5; 

if(3 * x * x <10)//〜75 < 10
{
doBlah(); // skipped
}

但是,如果X没有'const'值可能无法在编译时确定,并且可能泄漏到实际的最终产品中。



此外,下面代码中的expression的值不是必需的 - 时间常数:

  const int x = aFunction 

if(3 * x * x <10)//〜75 < 10
{
doBlah(); // skipped
}

X是一个常数,功能。 X很可能在编译时是不可确定的。在运行时,函数可以返回任何值*),所以编译器必须假设X是未知的。



因此,如果你有可能,那么使用预处理器。在微不足道的情况下,不会做太多,因为编译器已经知道了。但情况并不总是微不足道,你会注意到变化vrey经常。当优化器无法推导出这些值时,它会留下代码,即使它已经死了。另一方面,预处理器保证在编译和优化之前删除禁用的部分。此外,使用预处理器至少可以加快编译速度:编译器/优化器不必跟踪常量/计算/ checkbranches等。



*)编写一个返回值的方法/函数实际上可以在编译和优化阶段确定:如果函数是简单的,如果它被内联,它的结果值可能会与一些分支进行优化。但即使你可以有点依靠删除if-0子句,你不能依赖内联多少..


I am looking at code that someone else wrote, and it has a lot of debug sections, of type

if(0) { code }

or if(1) { code }

or if(false) { code }

There is even

#if(0)
#endif

(which did not turn gray though - I thought that it should)

I was wondering, if I replace these with some #if 0 (or #ifdef _DEBUG), is it possible to optimize the code ? - or - it will not make any difference ?

I think that it may help, since I have seen code that is within these sections being grayed out - and I thought that this code is removed from the Release executable... Therefore making it faster. Is that true ?

The code that I am thinking of is inside functions that could be called lots of times...

Edit: The code I am referring to is being run millions of times. I am aware that the contents of the if(0) will be ignored...

I am also aware of the benefit of being able to easily debug an issue, by switching a test from 0 to 1...

My question was, the fact that I am adding millions of millions of times the test if(0) does not add overhead... I am trying to figure out what are all the things that could make this code take fewer hours.

解决方案

If expressions placed inside those IFs are constant and determinable at the time of compilation, then you may be almost sure that the compiler has already removed them off the code for you.

Of course, if you compile in Debug-Mode, and/or if you have optimization-level set to zero, then the compiler may skip that and leave those tests - but with plain zero/one/true/false values it is highly unlikely.

For a compile-time constant branches, you may be sure that the compiler removed the dead ones.

It is able to remove even complex-looking cases like:

const int x = 5;

if( 3 * x * x < 10 ) // ~ 75 < 10
{
    doBlah(); // skipped
}

However, without that 'const' marker at X, the expression's value may be not determinable at the compile time, and it may 'leak' into the actual final product.

Also, the value of expression in following code is not necesarily compile-time constant:

const int x = aFunction();

if( 3 * x * x < 10 ) // ~ 75 < 10
{
    doBlah(); // skipped
}

X is a constant, but it is initialized with value from a function. X will most probably be not determinable at the time of compilation. In runtime the function could return any value*) so the compiler must assume that X is unknown.

Therefore, if you have possibility, then use preprocessor. In trivial cases that won't do much, because the compiler already knew that. But cases are not always trivial, and you will notice the change vrey often. When optimizer fails to deduce the values, it leaves the code, even if it is dead. Preprocessor on the other hand is guaranteed to remove disabled sections, before they get compiled and optimized. Also, using preprocessor to do that at least will speed up the compilation: the compiler/optimizer will not have to traceconstants/calculate/checkbranches etc.

*) it is possible to write a method/function which return value will actually be determinable at the compilation and optimization phases: if the function is simple and if it gets inlined, its result value might be optimized out along with some branches.. But even if you can somewhat rely on removing the if-0 clauses, you cannot rely on the inlining as much..

这篇关于c ++试图通过替换测试来优化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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