定义的参数计算顺序导致次优code? [英] Defined argument evaluation order leads to sub-optimal code?

查看:158
本文介绍了定义的参数计算顺序导致次优code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个众所周知的事实中的C和C这样的说法评估顺序++没有定义:
例如:美孚(A(),B())在上面的调用它是由编译器的实现来决定选择哪个评估的秩序,因此第四该函数首先执行。最近我的一个朋友问,为什么是评价C或C未指定的顺序++。当我GOOGLE了它,我才知道,指定评估顺序将导致次优code一代。但是怎么会这样呢?在规定的参数计算顺序为什么会导致次优code?当我提到Java的论证评估顺序。我发现,在规格如下。

It is a known fact that argument evaluation order in c and c++ are not defined: for example: foo(a(),b()) In the above call it is up to the implementation of the compiler to decide which order of evaluation to pick and hence forth which function to execute first. Lately one of my friends asked why is the order of evaluation unspecified in C or C++. When I googled it, I came to know that specifying an evaluation order would lead to sub-optimal code generation. But how is it so? Why would a defined order of evaluation of arguments lead to sub-optimal code? And when I referred to Java's argument evaluation order. I found the following in the spec.

15.7.4。参数列表评估左到右

15.7.4. Argument Lists are Evaluated Left-to-Right

在方法或构造函数调用或类实例的创建前pression,
      参数前pressions可能括号,用逗号分隔内出现。每个参数
      前pression出现任何参数前pression的任何部分前应充分评估
      这是正确的。
      如果参数前pression的评估完成后突然,没有任何参数的一部分
  前pression其右侧似乎已被评估?

In a method or constructor invocation or class instance creation expression, argument expressions may appear within the parentheses, separated by commas. Each argument expression appears to be fully evaluated before any part of any argument expression to its right. If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated?

既然如此,Java已经定义的参数计算顺序,而是说如果指定这样的行为C或C ++编译器会产生次优code似乎有点古怪。你可以扔掉一些轻对此有何看法?

That being the case, Java has a defined argument evaluation order, but saying C or C++ compilers would yield sub-optimal code if such a behavior is specified seems a little odd. Can you throw some light on this?

推荐答案

这部分的历史:上很少寄存器,用于处理器
例如,一个传统的(和简单的)优化的技术是
评估SUBEX pression它首先最需要的寄存器。如果一个人
SUBEX pression需要5个寄存器,另4,例如,您
可以保存一个在寄存器要求5不需要的结果
通过要求一个4.

It's partially historical: on processors with few registers, for example, one traditional (and simple) optimization technique is to evaluate the subexpression which needs the most registers first. If one subexpression requires 5 registers, and the other 4, for example, you can save the results of the one requiring 5 in the register not needed by the one requiring 4.

这是不太可能的相关通常被认为。编译器可以
重新排序(即使在Java中),如果前pressions没有副作用,或者
重排序不改变程序的可观察到的行为。
现代编译器能够determing这远比更好的编译器
二十或更多年前(当C ++规则是制定)。和
presumably,当他们无法确定这一点,你做得还不够
在每一个前pression,额外的溢出内存无所谓。

This is probably less relevant that usually thought. The compiler can reorder (even in Java) if the expressions have no side effects, or the reordering doesn't change the observable behavior of the program. Modern compilers are able to determing this far better than compilers twenty or more years ago (when the C++ rule was formulated). And presumably, when they aren't able to determine this, you're doing enough in each expression that the extra spill to memory doesn't matter.

至少,这是我的直觉。我已经告诉至少一人
究竟是谁在运作优化,这将使一个显著
差,所以我不会说我敢肯定它。

At least, that's my gut feeling. I've been told by at least one person who actually works on optimizers that it would make a significant difference, so I won't say that I'm sure about it.

编辑:

只是为了与问候Java模型添加一些评论。将Java
被设计的,它被设计为一个跨preTED的langauge。极端
性能是不是一个问题;目标是极度安全性,
可重复性。因此,它指定很多东西很precisely,所以
这里面编译任何程序都会有完全一样的行为
无论哪一个平台。目前应该是没有未定义
行为,没有实现定义的行为,也没有不明
行为。不计成本的(但相信这可能是
在任何最wides $ P $垫块机的合理费用)来完成。一
C(间接C ++)的最初设计目标是不必要的
额外的运行成本应该最低,平台之间的一致性
是不是一个目标(因为在时间,甚至共同的平台变化
大大),并且安全,同时关注的问题,不是原始。而
态度已经进化一些,仍然能够一个目标
支持,有效,这可能是在那里任何机器。没有
需要最新,最复杂的编译器技术。而不同的
目标自然会导致不同的解决方案。

Just to add some comments with regards to the Java model. When Java was being designed, it was designed as an interpreted langauge. Extreme performance wasn't an issue; the goal was extreme safety, and reproduceability. Thus, it specifies many things very precisely, so that any program which compiles will have exactly the same behavior regardless of the platform. There was supposed to be no undefined behavior, no implementation defined behavior, and no unspecified behavior. Regardless of cost (but with the belief that this could be done at reasonable cost on any of the most widespread machines). One initial design goals of C (and indirectly C++) was that unnecessary extra runtime cost should be minimum, that consistency between platforms wasn't a goal (since at the times, even common platforms varied greatly), and that safety, while a concern, wasn't primordial. While the attitudes have evolved some, there is still a goal to be able to support, efficiently, any machine which might be out there. Without requiring the newest, most complex compiler technologies. And different goals naturally lead to different solutions.

这篇关于定义的参数计算顺序导致次优code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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