简体   繁体   English

额外的代码块——范围——会减慢执行速度吗?

[英]Do extra code blocks—scopes—slow down execution?

Whenever I comment out my code—I like to have nice and big comments.每当我注释掉我的代码时——我喜欢有好的和大的注释。 Upon times the big comments need be nested, I usually add extra indentation.有时需要嵌套大注释,我通常会添加额外的缩进。 It would—however—be a lot clearer should I be able to use extra code blocks {} to make the indentation more logical.但是,如果我能够使用额外的代码块{}来使缩进更合乎逻辑,那将会更加清晰。

Let's have a quick example:让我们举一个简单的例子:

// ┌───────────────────┐
// │ COMMENT           │
// └───────────────────┘
{
    // Code in here
}
// ┌───────────────────┐
// │ ;                 │
// └───────────────────┘

Will the {} in this example slow down the speed of a Java program in any way or will they be automatically removed by the compiler?此示例中的{}会以任何方式减慢 Java 程序的速度还是会被编译器自动删除? Surely {} is a scope in and of itself, and Java would want to keep that, correct?当然, {}本身就是一个作用域,Java 想要保留它,对吗?

If your methods are complex and difficult to understand in one go, try extracting one or more methods from your large method.如果您的方法复杂且难以一次性理解,请尝试从大型方法中提取一种或多种方法。 Note: methods calls are not free, but smaller methods tend to get inlined so you can end up with code which is just as fast even with method calls in your code.注意:方法调用不是免费的,但较小的方法往往会被内联,因此即使在代码中调用方法,您也可以得到同样快的代码。

Surely {} is a scope in and of itself, and Java would want to keep that, correct?当然,{} 本身就是一个作用域,Java 想要保留它,对吗?

The byte code can have frame information but rarely does.字节码可以有帧信息,但很少有。 Usually there is no way to know from the byte code whether you have used extra { x(); }通常没有办法从字节码中知道你是否使用了 extra { x(); } { x(); } or (x) in your code. { x(); }(x)在您的代码中。

These frames may or may not make a different to the machine code actually generated, but if they are not there, they make no difference.这些帧可能会或可能不会与实际生成的机器代码不同,但如果它们不存在,它们就没有区别。

Most of the time, the most important thing is clarity, you should worry about performance when you have measured you have a problem.大多数时候,最重要的是清晰度,当你测量出问题时,你应该担心性能。 The only time you need to worry up front is when time complexity matters, use a strategy with a lower time complexity.您唯一需要预先担心的是当时间复杂度很重要时,请使用时间复杂度较低的策略。

Four different placement directions need be checked per self-contained method.每个独立的方法需要检查四个不同的放置方向。

In that case I would have a loop like在那种情况下,我会有一个循环

enum Direction {
    NORTH(0, +1), SOUTH(0, -1), EAST(+1, 0), WEST(-1, 0);

    final int x, y;

    Direction(int x, int y) { this.x = x; this.y = y; }
}

for (Direction dir : Direction.values()) {
    // do something with each direction.
}

You can store Direction.values() in a private constant to save a little garbage/allocation.您可以将Direction.values()存储在私有常量中以节省一些垃圾/分配。

They won't really slow down anything, as they will be removed by the compiler.它们不会真正减慢任何东西,因为它们会被编译器删除。

However, rather than writing code like that, you might want to try and extract a function with a good , self-documenting name.但是,与其编写这样的代码,不如尝试提取一个具有良好、自文档化名称的函数。 It will be much more readable.它将更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM