Java编译器优化重复的方法调用? [英] Java compiler optimization for repeated method calls?

查看:139
本文介绍了Java编译器优化重复的方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java编译器(JDK1.6.0_21中的默认javac)是否优化代码,以防止使用相同的参数重复调用相同的方法?如果我写的代码:

Does the java compiler (the default javac that comes in JDK1.6.0_21) optimize code to prevent the same method from being called with the same arguments over and over? If I wrote this code:

public class FooBar {
    public static void main(String[] args) {
        foo(bar);
        foo(bar);
        foo(bar);
    }
}

方法 foo bar)只运行一次?如果是这样,有没有办法防止这种优化? (我试图比较两个algos,一个迭代和一个比较的运行时,我想多次调用它们来获得一个有代表性的样例)

Would the method foo(bar) only run once? If so, is there any way to prevent this optimization? (I'm trying to compare runtime for two algos, one iterative and one comparative, and I want to call them a bunch of times to get a representative sample)

Any洞察将非常感激;我把这个问题的疯狂点(我虽然我的计算机是疯狂的一段时间,所以我一直在添加方法调用,直到我得到代码太大错误在43671行)。

Any insight would be much appreciated; I took this problem to the point of insanity (I though my computer was insanely fast for a little while, so I kept on adding method calls until I got the code too large error at 43671 lines).

推荐答案

您观察到的优化可能与重复调用无关...因为这将是无效的优化。更可能的是,优化器已经弄清楚方法调用对计算没有可观察到的影响。

The optimization you are observing is probably nothing to do with repeated calls ... because that would be an invalid optimization. More likely, the optimizer has figured out that the method calls have no observable effect on the computation.

解决方法是改变方法,以确保影响结果计算...

The cure is to change the method so that it does affect the result of computation ...

这篇关于Java编译器优化重复的方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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