What are the most efficient sizes for Variables/Arrays?

What im asking exactly is, which would be the most efficient sizes for Arrays. For numbers from 0 - 2^32 numbers it would be an int, for 0 - 2^64 it would be long, but how does that work for arrays? A perfekt answer would be a list like:
Array[8] with ints would be more efficient, than Array[7] with ints. I don’t know if thats really how it works, so this might not even be the case and all array sizes are equal. But since using a long for a number range of 0-7 is pretty inefficient, i just wanted to be sure. What i would need would be a very high arraysize (~10.000-100.000) with a class that contains a PVector and a byte/char array of size 8 and an byte/char variable. Now the question, what arraysize would be best? Would it matter if the size is something like 2^16 (65.536) or is it as good as 60.000 or 66.000?

1 Like

I get what you’re asking, but that’s not how it works. If you need an array of 10000 things, then the best array size is 10000. Rounding up to a power of two isn’t going to make a difference. Your system is already doing a bunch of memory management tricks (like cashing and paging) to deal with data already.

Basically, for the best performance, use the smallest array size you need. For better performance, see if you can get by with a smaller array.

1 Like

The problem lies in that the array size is actually pretty much secondary. It can be anything between 10.000 (because i need a lot) and 100.000 (because my system will hate me, if its more). And since memory/performance wise its best to use shorts if you only have 2^16 number range, i thought same would go with arrays. Though i don’t know how they are saved… I know chars are 8 bit(1byte) shorts are 16, ints are 32 and longs are 64. But would a char array of size 8 be like… 8 chars or 8 byte. And with size 7 it would be 7 byte, so nothing that could be improved performance wise… yeah… should’ve thought about it a bit more xD… Ok, i’ll just search for the best size then^^ Thx for the answer^^

Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Per what GoToLoop posted, a char takes up 2 bytes of memory (16 bits).

Thus, if you have an array of 10 chars, you are using (10 chars) * (2 byte per char) = 20 bytes total.
If you have an array of 9 chars, you are using (9 chars) * (2 byte per char) = 18 bytes total.

You can’t change how many bytes a type uses (without serious hacking) [citation needed], but you can control how large your arrays are.

2 Likes

Yeah, i was more thinking that maybe if you have an array of a specific size it could somehow use the excess number of bits on another number that is smaller, but, even though it might be possible to save data this way, it would pretty much be unreadable. Like say you have an array of 3 chars, so 6 bytes, what i thought could happen (i was aware how it more or less works, but just to be sure i asked^^) would be that lets say numbers saved are 8, 20 and 30000 then the amount used to save those numbers could be limited to 3 for 8, 5 for 20 and 15 for 30000 ending up with 3+5+15 bits used, so you could just save this in 23 bits or 3 bytes, but then i realized, that you would need to know the specific size of each number, and if you know that, you could just know the exact number xD. But it might be useful if you have an array where numbers range from 0-longs or llarger numbers xD