# [Solved]Bigger Arrays VS Multiple Variables

Status
Not open for further replies.

#### MaXaRaYkIn

Level 4
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:

#### Garfield1337

Level 20
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.

#### MaXaRaYkIn

Level 4
Ok... What if I chose...

Name: "Time1", "Time2", "Time3", "Time4", ..., "Time20"
Type: Integer
Array: No

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

Will the array "Time" be more convenient instead of 20 normal variables?

#### MaXaRaYkIn

Level 4
Obvious or not I just wanted to be sure...

#### deathismyfriend

Level 29
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.

#### Arhowk

Level 19
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

#### edo494

Level 23
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

#### Garfield1337

Level 20
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.

#### PurgeandFire

Level 44
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.

#### deathismyfriend

Level 29
Thanks everyone i knew i was a little off.

#### Garfield1337

Level 20
@Purgeandfire,
That's surprising, I didn't know that
Does that mean that the size increases during execution if you access higher indices?

#### edo494

Level 23
yes, exactly, if you try to use index 1255(read or write), the game will detect it if the array is already not of the size, and it will resize it to the closest higher power of two(2048)

Level 4
Thanks everyone.

#### Garfield1337

Level 20
yes, exactly, if you try to use index 1255(read or write), the game will detect it if the array is already not of the size, and it will resize it to the closest higher power of two(2048)

That's cool. Sorry for misinforming in my previous post, I have always seen arrays being referred to as static.

Status
Not open for further replies.

Replies
9
Views
1K
Replies
5
Views
843
Replies
3
Views
1K
Replies
8
Views
3K
Replies
8
Views
2K