#5619 - punchy - Sat May 03, 2003 8:08 pm
Hi,
Hope this isn't too stupid a question.
Say I have a large array of data which is used in a drawing function. Is declaring it globally a better idea, for execution speed reasons, than declaring it in the function it will be used in. Eg
int array [1000];
int main(){
DrawScreen();
}
VS
int main(){
DrawScreen(); //
}
void DrawScreen(){
int array [1000];
//Drawing code
}
Will this data be pushed on the stack every call or will the compiler be smart. Do the optimisation flags affect this?
Thanks.
#5702 - Sweex - Tue May 06, 2003 1:00 pm
I don't know for sure, but I think the compiler will not be "smart" enough to put your int array [1000]; in the "global space".
Why I think that? Because that is a fundamental change of the actual meaning of the code.
(Isn't accessing the stack faster than accessing global memory?)
#5705 - Quirky - Tue May 06, 2003 3:00 pm
They would both go into IWRAM by default.
One thing you have to worry about if you place it on the stack is that you can't use ld's -Map feature to see where exactly in RAM the array goes, which is useful for debugging (and for seeing how much IWRAM you use). There would also be some extra assembler code added at compile time for accessing the stack-based array too, though that would probably only be noticed as a slightly larger final rom more than anything.
Something like that is probably better placed as a global though. It's what I would do with it.
#5726 - jd - Wed May 07, 2003 1:40 am
Quirky wrote: |
Something like that is probably better placed as a global though. It's what I would do with it. |
Whilst I agree with you in the case where you want to know the exact address so you can check it in an emulator (although perhaps displaying the address at run-time might be a better solution), you shouldn't usually use a global variable in the situation above for the following reasons:
1) Unlike a local variable, a global variable will still use the IWRAM even outside the routine.
2) Accessing a global means getting its address. Local variables don't require this step because the address of the stack is already in r13.
3) The optimiser knows when it can safely put local variables in registers - the same isn't true of globals so it has to err on the side of caution.
Remember that allocating space on the stack takes virtually no time at all - it's just a single subtraction.
#5738 - Quirky - Wed May 07, 2003 2:23 pm
jd wrote: |
Quirky wrote: |
Something like that is probably better placed as a global though. It's what I would do with it. |
you shouldn't usually use a global variable in the situation above for the following reasons <snip> |
There's nothing like putting my foot in it to learn new things ;)
#5743 - punchy - Wed May 07, 2003 10:09 pm
Thanks for the replies. There are still a couple of things i'd appreciate if anyone could clarify for me.
When I declare my array locally
void DrawScreen(){
int array [1000];
//Drawing code
}
What actually gets pushed onto the stack. Does the stack pointer just get moved 1000 array sized elements? Hence "Remember that allocating space on the stack takes virtually no time at all - it's just a single subtraction."
Now, say this array of 1000 elements gets initialised with values. Say graphics data for example. Now if the array is inside the DrawScreen function these 1000 values will have to be set. This I imagine will be quite a time consuming process. And it will happen every time the function is called.
In this case is it better to use global data, as these values will only be set once, when they are written to the global section of IWRAM.
Thanks for all your help.
#5746 - jd - Wed May 07, 2003 11:43 pm
punchy wrote: |
What actually gets pushed onto the stack. Does the stack pointer just get moved 1000 array sized elements? Hence "Remember that allocating space on the stack takes virtually no time at all - it's just a single subtraction."
|
Yes.
punchy wrote: |
Now, say this array of 1000 elements gets initialised with values. Say graphics data for example.
|
In the case of fixed graphics data you should really be using const. But I digress. :)
punchy wrote: |
Now if the array is inside the DrawScreen function these 1000 values will have to be set. This I imagine will be quite a time consuming process. And it will happen every time the function is called.
|
Yes.
punchy wrote: |
In this case is it better to use global data,
|
That depends on whether the function relies on the values being reset back to their initial values each time the code is called - if it does, then making the array a global (or, preferably, a static local) would break the routine.
#5771 - punchy - Thu May 08, 2003 9:45 am
As the data does not ever change i'll just make it a const static local.
Thanks for all your help.