且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java速度访问数组索引与临时变量

更新时间:2023-02-10 15:42:51

第二种方法肯定更快。但是你可以使用 final 关键字提供更多帮助:

The second approach is definitely faster. But you can help even more with the final keyword:

final float x = shape.vertices[0].x;
final float y = shape.vertices[0].y;
final int rightEdge = x + shape.width;
if ((x >= fromX && x <= toX) || // left side of shape in screen
(x <= fromX && rightEdge >= fromX) || // right side of shape in screen
(x >= fromX && rightEdge <= toX)) { // shape fully in screen

    // ...
}

当然不是一个显着的改进(但仍然是一个改进,也是使意图明确)。您可以阅读以下讨论: http:/ /old.nabble.com/Making-copy-of-a-reference-to-ReentrantLock-tt30730392.html#a30733348

Not a significant improvement of course (but still an improvement and also makes the intent clear). You can read this discussion: http://old.nabble.com/Making-copy-of-a-reference-to-ReentrantLock-tt30730392.html#a30733348