简体   繁体   中英

Which is a more efficient integer array size in Java

Okay, so I'm working on a custom data structure which uses a fairly large number of arrays to efficiently stored a lot (millions of entries) efficiently, but I got to wondering; what is the most efficient way to size the arrays?

My natural inclination is to pick array sizes using binary powers, eg - sizes of 4, 8, 16 etc., but since Java arrays also include their length, should I count that as if it were an additional element, ie - if I'm storing an array of integers, should I be using sizes of 3, 7, 15 etc. instead, to account for the extra integer?

I realise for most cases this is probably academic, but selecting new array sizes is already its own function in my code, so it'd be dead easy to tweak, plus I'm interested to know.

The only thing the array size affects is when the array is full. Unlike for example hash tables, where you frequently need to perform modulo operations (which can be optimized if you know that the size is a power of two), an ordinary array only needs to compare the size, and comparison is uniformly fast for all values.

Different initial capacities and different growth factors might nevertheless affect program performance or memory consumption, but only because some growth patterns "fit" some uses better, in that they better balance the space wasted on over-allocation and the time for resizing (both are amortized O(1), but still subject to hidden constant factors). However, this depends entirely on how a typical program behaves on typical inputs. It's not something you can decide in isolation by staring at the array implementation.

In the absence of more data, you should just pick an arbitrary but reasonable starting capacity (somewhere between 1 and 30 I'd say) and growth factors (somewhere between 1.5 and 3 I'd say). At worst it decreases performance by a constant factor, and a rather small one at that. Or let the user pick it, if you want to make the API slightly more complicated.

I asked basically the same question here: Determine the optimal size for array with respect to the JVM's memory granularity (but got no useful responses)

The main problem in chosing the perfect array size is that it requires knowledge how the VM actually lays out an array in memory, and can't be pre-determined because it depends a lot on platform architecture, but also VM version and possibly VM parameters (eg Compressed OOP on/off with 64-bit).

You could look up the granularity for typical VM settings though or simply hardcode reasonable assumptions into your array size selection.

Edit: There is a solution, but its not portable (using Unsafe class): http://openjdk.java.net/projects/code-tools/jol/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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