强制编译器不优化副作用更少的语句 [英] Force compiler to not optimize side-effect-less statements

查看:104
本文介绍了强制编译器不优化副作用更少的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一些旧的游戏编程书,并且你们中的一些人可能知道,在那一天,通常比使用标准方式做的事更快。 (将 float 转换为 int ,掩码符号位,转换回绝对值,而不是调用 fabs(),例如)



现在几乎总是更好地使用标准库数学函数,因为这些微小的东西几乎不是大多数瓶颈的原因。



但我仍然想做一个比较,只是为了好奇的缘故。所以我想确保当我配置文件,我不会得到扭曲的结果。因此,我想确保编译器不优化没有副作用的语句,例如:

  void float_to_int(float f)
{
int i = static_cast< int>(f); //没有副作用
}

有办法吗?据我所知,做一些 i + = 10 仍然没有副作用,因此不会解决问题。



我可以想到的唯一的事情是有一个全局变量, int dummy; ,然后在做类似 dummy + = i ,因此使用 i 的值。但我觉得这个虚拟操作将会阻碍我想要的结果。



我使用Visual Studio 2008 / G ++(3.4.4) p>

编辑



为了阐明这一点,我希望所有优化最大化,以获得良好的配置文件结果。问题是,没有副作用的语句将被优化,因此会出现这种情况。



再次编辑



为了再次阐明,请阅读:

b

我们都知道旧的技巧不再有用了,我只是好奇 不是有用的。只是普通的好奇心。当然,生活可以继续下去,我不知道这些旧的黑客如何对现代的CPU执行,但它从来没有伤害知道。



所以告诉我这些技巧aren' t有用了,停止尝试微优化blah blah是一个答案完全错过了点。



Knuth的过早引用是所有烦恼的根源。

p>

解决方案

编译器不幸被允许进行优化,即使没有任何明确的开关,如果代码的行为,地点。但是,如果您指示该值可能稍后可以使用,您通常可以诱使他们不这样做,因此我会将您的代码更改为:

  int float_to_int(float f)
{
return static_cast< int>(f); //没有副作用
}

正如其他人建议的,检查assemnler输出以检查此方法是否实际工作。


I was reading some old game programming books and as some of you might know, back in that day it was usually faster to do bit hacks than do things the standard way. (Converting float to int, mask sign bit, convert back for absolute value, instead of just calling fabs(), for example)

Nowadays is almost always better to just use the standard library math functions, since these tiny things are hardly the cause of most bottlenecks anyway.

But I still want to do a comparison, just for curiosity's sake. So I want to make sure when I profile, I'm not getting skewed results. As such, I'd like to make sure the compiler does not optimize out statements that have no side effect, such as:

void float_to_int(float f)
{
    int i = static_cast<int>(f); // has no side-effects
}

Is there a way to do this? As far as I can tell, doing something like i += 10 will still have no side-effect and as such won't solve the problem.

The only thing I can think of is having a global variable, int dummy;, and after the cast doing something like dummy += i, so the value of i is used. But I feel like this dummy operation will get in the way of the results I want.

I'm using Visual Studio 2008 / G++ (3.4.4).

Edit

To clarify, I would like to have all optimizations maxed out, to get good profile results. The problem is that with this the statements with no side-effect will be optimized out, hence the situation.

Edit Again

To clarify once more, read this: I'm not trying to micro-optimize this in some sort of production code.

We all know that the old tricks aren't very useful anymore, I'm merely curious how not useful they are. Just plain curiosity. Sure, life could go on without me knowing just how these old hacks perform against modern day CPU's, but it never hurts to know.

So telling me "these tricks aren't useful anymore, stop trying to micro-optimize blah blah" is an answer completely missing the point. I know they aren't useful, I don't use them.

Premature quoting of Knuth is the root of all annoyance.

解决方案

Compilers are unfortunately allowed to optimise as much as they like, even without any explicit switches, if the code behaves as if no optimisation takes place. However, you can often trick them into not doing so if you indicate that value might be used later, so I would change your code to:

int float_to_int(float f)
{
    return static_cast<int>(f); // has no side-effects
}

As others have suggested, you will need to examine the assemnler output to check that this approach actually works.

这篇关于强制编译器不优化副作用更少的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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