向方法添加局部变量是否会使它们变慢?

发布于 2024-11-02 20:13:14 字数 1399 浏览 0 评论 0原文

这个问题一共得到了好几段的回答。这是唯一真正告诉我我在寻找什么的句子:

您的示例几乎没有什么区别,因为中间计算需要临时存储在堆栈上,以便稍后使用。

事实上,它完美、完整地回答了我的问题 =)

不像所有那些告诉我“不,不要问这个问题”的东西。 >_<


就像如果您有一个方法,并且通过增加局部变量的数量来更改它但不进行其他更改,这是否会使该方法变慢?这是一个例子:

void makeWindow() {
    Display
        .getContext()
        .windowBuilder()
        .setSize(800, 600)
        .setBalloonAnimal(BalloonAnimal.ELDER_GOD.withColor(PUCE))
        .build();
}

或者

void makeWindow() {
    DisplayContext dc = Display.getContext();
    WindowBuilder wb = db.windowBuilder();
    BalloonAnimal god = BalloonAnimal.ELDER_GOD;
    BalloonAnimal puceGod = god.withColor(PUCE);
    wb.setSize(800, 600).setBalloonAnimal(puceGod).build();
}

另一个例子:

int beAnExample(int quiche) {
    return areGlobalsEvil?
        quiche * TAU/5:
        highway(quiche, Globals.frenchFrenchRevolution);
}

或者

int beAnExample(int quiche) {
    if (areGlobalsEvil) {
        int to5 = TAU/5;
        int result = quiche * to5;
        return result;
    } else {
        Game french = Globals.frenchFrenchRevolution;
        int result = highway(quiche, french);
        return result;
    }
}

真的,我要问的是:这种局部变量的数量在方法编译为字节码时是否相关?如果是这样,一旦 Hotspot 开始工作怎么办?

这个问题与我正在开发的代码生成器相关。

This question has received a total of several paragraphs of answer. Here is the only sentence that actually tells me what I was looking for:

Your examples would make little difference since intermediate computations need to be stored temporarily on the stack so they can be used later on.

In fact, it answers my question perfectly and completely =)

Unlike all the cruft telling me "nooo don't ask that question". >_<


Like if you have a method, and you change it by increasing the number of local variables but make no other changes, does it make the method slower? Here's an example:

void makeWindow() {
    Display
        .getContext()
        .windowBuilder()
        .setSize(800, 600)
        .setBalloonAnimal(BalloonAnimal.ELDER_GOD.withColor(PUCE))
        .build();
}

or

void makeWindow() {
    DisplayContext dc = Display.getContext();
    WindowBuilder wb = db.windowBuilder();
    BalloonAnimal god = BalloonAnimal.ELDER_GOD;
    BalloonAnimal puceGod = god.withColor(PUCE);
    wb.setSize(800, 600).setBalloonAnimal(puceGod).build();
}

Another example:

int beAnExample(int quiche) {
    return areGlobalsEvil?
        quiche * TAU/5:
        highway(quiche, Globals.frenchFrenchRevolution);
}

or

int beAnExample(int quiche) {
    if (areGlobalsEvil) {
        int to5 = TAU/5;
        int result = quiche * to5;
        return result;
    } else {
        Game french = Globals.frenchFrenchRevolution;
        int result = highway(quiche, french);
        return result;
    }
}

Really, what I'm asking is: Is the number of this sort of local variable even relevant by the time the method's compiled to bytecode? If so, what about once Hotspot gets to work on it?

This question is relevant to the code generator I'm working on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑶笙 2024-11-09 20:13:14

简单的答案是否定的。局部变量消耗运行时堆栈空间。为它们分配空间只会略微增加指令数量。您的示例几乎没有什么区别,因为中间计算需要临时存储在堆栈上,以便稍后使用。更多地关注程序的可读性,而不是不必要的微观优化。

如果您有兴趣查看类的实际字节码,请研究 javap 程序。

The easy answer is no. Local variables consume runtime stack space. Allocating space for them only marginally increases the number of instructions. Your examples would make little difference since intermediate computations need to be stored temporarily on the stack so they can be used later on. Focus more on the readability of your programs rather than needless micro-optimizations.

If you're interested in looking at the actual bytecode of a class, investigate the javap program.

情深缘浅 2024-11-09 20:13:14

不用担心。编译器可以进行各种疯狂的、令人头疼的优化。从正确且可维护的代码开始。程序员的时间比处理器时间更有价值。

Don't worry about it. The compiler can do all sorts of crazy, make-your-head-asplode optimizations. Start with code that's correct and maintainable. Programmer time is worth far more than processor tiem.

z祗昰~ 2024-11-09 20:13:14

通过运行每个方法 1,000,000 次来测试它,然后除以总时间来计算每次执行的成本。很可能,它不会引人注目。

实际上,Java 编译器甚至可能足够聪明,可以将其编译出来。

编写代码以提高可读性,从而降低长期维护成本。然后在你真正需要的 5% 的地方进行调整。

Test it by running each method 1,000,000 times and divide the total time to calculate the cost per execution. In all likelihood, it won't be noticable.

Actually, Java compilers may even be smart enough to just compile it out.

Write your code for readability to reduce long term cost of maintenance. Then tune it in the 5% of places where you really need to.

看海 2024-11-09 20:13:14

很可能它不会产生什么(如果有的话)差异,并且“一点”将是微不足道的。

专注于使你的生成器正确且可维护,并让Java编译器(特别是JIT编译器)对生成的代码进行微优化。

请注意,@Edawg 关于查看字节码的建议不一定有帮助。 JIT 编译器积极优化它从字节码生成的本机代码。预测两个字节码序列中哪一个更快可能很困难。当然,计算字节码、方法调用等可能会产生误导。

确保生成“最佳”Java 源代码的唯一方法是编译它并在目标平台上对其进行基准测试。即使如此,您在源代码级优化方面所做的努力也很有可能会因为 JIT 编译器的更改而被抵消。

The chances are that it will make little (if any) difference, and the "little" will be insignificant.

Focus on making your generator correct and maintainable, and let the Java compiler (particularly the JIT compiler) do the micro-optimization of the generated code.

Note that @Edawg's advice on looking at the bytecode is not necessarily helpful. The JIT compiler aggressively optimizes the native code that it generates from the bytecodes. It can be difficult to predict which of two bytecode sequences is going to be faster. Certainly, counting bytecodes, method calls and so on can be misleading.

The only way to be sure that you are generating "optimal" Java source code would be to compile it and benchmark it on your target platform. Even then, there's a good chance that your efforts at source-code level optimization will be negated ... by JIT compiler changes.

另类 2024-11-09 20:13:14

这不是热点问题。可能需要额外的字节码来加载和存储局部变量,但让编译器担心优化它。

您应该专注于生成代码中的空指针检查以及如何以与生成代码的源输入相关的有意义的方式报告错误等问题。

This is not a hotspot issue. There may need to be additional byte codes to load and store the local variables, but let the compiler worry about optimizing that.

You should concentrate on issues like null pointer checking in your generated code and how to report errors in a meaningful way that is tied to the source input that you are code generating from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文