开发者

Is it more efficient to access array elements through the array than to get them through a function?

I'm thinking about making an object that consists of a large grid, stored in a two dimensional array, and functions that work on that grid.

If I want to iterate through the elements in this array outside of the object,开发者_StackOverflow中文版 the most readable and privacy-respectful way to access each grid element would be to use a function like grid.getElement(x,y), which simply returns array[x][y].

When the program is compiled into bytecode, is this any less efficient than directly accessing the array by means of grid.array[x][y]?


It's potentially less efficient, but it depends on your JVM. A good JIT may be able to inline the function, thereby making the code exactly equivalent.

The only way to know for sure is to benchmark with the JVM that you'll be using. Before you even do that you should verify that it even matters. (ie: profile then optimize) Unless your profiling tells you that a particular piece of code is a bottleneck, go with the cleanest design, and don't try to micro-optimize like this. There's a good chance that it wouldn't buy you any measurable performance gain anyway, and it'll definitely make your code harder to maintain.


It is probably slightly less efficient, but you should let the compiler and JIT compiler do the optimizations for you.

You should be concerned mainly with readability and effectivity of your API, unless you actually find some performance bottleneck.


Performance considerations aside, the mechanism of access via function is certainly "cleaner" and "safer." For one thing, you can expose the array elements as read-only if you don't supply a modifying function. By not providing a reference to the array, you're also preventing other and perhaps unwanted accesses to the array; and finally, by not exposing the array, you're keeping your API flexible with respect to future modifications, i.e. replacing the array with a different data structure. These are all benefits with respect to the quality of your code, and should in most cases weigh more heavily than a small loss in performance incurred by wrapping access in a function.


It's hard to say for sure, and I would profile if this is something where performance is sufficiently important to be worth sacraficing good design for.

The JIT will inline simple functions, such as getters. And the JIT can also eliminate the array bounds checks in many cases. So, putting the array access behind a method may not have worse performance than accessing it directly if these optimizations are done, but if they are not done, then performance may suffer.

But you may find that if the arrays are large enough, the function call overhead is much less than the cpu<>memory bottleneck, and choice of algorithm, such as using "block" algorithms on matrices will make the biggest difference, and not so much how the individual elements are accessed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜