• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Bigger Arrays VS Multiple Variables

Status
Not open for further replies.
Level 4
Joined
Apr 25, 2011
Messages
73
I don't know if this has been answered (I just have a really hard time with search things...), but I will just get to the point:

I was wondering...

Let's say we have variables:

Name: "Time"
Type: Integer
Array: Yes (20 max)

Name: "Time1"
Type: Integer
Array: Yes (10 max)

Name: "Time2"
Type: Integer
Array: Yes (10 max)

Let's assume that we have 20 random (the value is not important) numbers. First we get the variable "Time" to hold the values in every array slot. Then we get "Time1" to hold the first 10 values and "Time2" to hold the other 10 values.

Now... What is consuming less memory? The bigger array "Time" which is only one, but has more array slots or the two variables "Time1" and "Time2" which have less array slots?
 
Last edited by a moderator:
Level 20
Joined
Jul 6, 2009
Messages
1,885
You can't set array size, it's 2^13 for every array. The option in variable editor, I think it's called size, only determines the index up to which the values will be initialized with defined starting value.
So using 2 arrays will take 2 times more memory, which means it's better to use 1.
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
The array Time will be more convenient yes. It will take more memory i believe but its such a small amount of memory that you shouldn't worry about it unless your map has close to 300k variables in it.

as long as u set the "max value" to 1, arrays dont take up that much memory to the point that its insignificant. if every 32 bits of all 8192 integers were consumed, than each variable array would be 266kb
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
You can't set array size, it's 2^13 for every array. The option in variable editor, I think it's called size, only determines the index up to which the values will be initialized with defined starting value.
So using 2 arrays will take 2 times more memory, which means it's better to use 1.

not totally true, as Jass arrays work very similarly to vector in C++.

It is dynamically allocated by powers of 2, so if you try to use index 998, the array will be allocated for up to 1023th index(1024 elements).

So if you are only using lets say 0, 1, 2, 3, 4, 5, your array should be only occupy 8x more space than single variable

If the statement above is incorrect, let me know, and I will remove it. Im not 100% certain, but people have said this in past as well

overall, you dont need to bother with it too much. Just put intial size to 1 on every array variable, otherwise the initialization of map will not run successfully
 
Level 20
Joined
Jul 6, 2009
Messages
1,885
not totally true, as Jass arrays work very similarly to vector in C++.

It is dynamically allocated by powers of 2, so if you try to use index 998, the array will be allocated for up to 1023th index(1024 elements).

So if you are only using lets say 0, 1, 2, 3, 4, 5, your array should be only occupy 8x more space than single variable

If the statement above is incorrect, let me know, and I will remove it. Im not 100% certain, but people have said this in past as well

overall, you dont need to bother with it too much. Just put intial size to 1 on every array variable, otherwise the initialization of map will not run successfully

They aren't dynamically allocated, so no. The size is always 2^13.
 
Just fyi, according to this:
http://www.wc3c.net/showthread.php?p=1084530

Arrays take up memory in powers of 2 as the indexes used becomes higher. With 10 slots used, (at least according to that link), you would have (2^4)*4 bytes = 16 * 4 = 64 bytes (integers are 32 bit -> 4 bytes). With two of those, you'd have 64 * 2 = 128.

With a single array, 20 slots, you'd use (2^5)*4 bytes = 32*4 = 128 bytes as well. So you're comparing apples to apples.

Use whichever one is more convenient. There may be more memory that is associated with arrays internally (I wouldn't know since I haven't tested that). However, the difference is likely very negligible. Use whichever is more convenient. That's what arrays are for--it eases iteration and lets you keep things compact. It is not worth it to clog your variable editor with unnecessary variables.

If it is easier for you to use multiple variables (or if it makes sense for you to use multiple variables), then go for it. Memory isn't for min-maxing--you should be efficient about it (remove memory leaks, don't have unnecessary things stored, etc.)--but don't go overboard to save a few kb of memory.

edit: 3 posts happened while I was posting this, lol. Although, suddenly my post is relevant.

edit2: And one thing that is key--it allocates in powers of 2. Even if you don't use the slots, it'll still allocate memory space for it.
 
Status
Not open for further replies.
Top