View previous topic :: View next topic |
Author |
Message |
s_mack
Joined: 04 Jun 2009 Posts: 107
|
Memory management... can scope of vars make a difference? |
Posted: Tue Aug 11, 2009 10:26 am |
|
|
I'm running out of ROM (especially) and RAM (to a lesser degree) in my firmware.
Device: PIC18F4480
I'm just wondering if the scope of variables can make any appreciable difference in memory use. For example:
Code: |
float x = 2.03; //declared globally
void test_function()
{
x = x * 2;
}
void main()
{
while (1)
{
test_function();
if (x>10)
{
//whatever
}
}
}
|
versus...
Code: |
float test_function(float _var)
{
return _var * 2;
}
void main()
{
float x = 2.03;
while (1)
{
x = test_function();
if (x>10)
{
//whatever
}
}
}
|
Ok, stupid code sure... but is there any difference in theory? Thanks. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Aug 11, 2009 11:09 am |
|
|
Did you try compiling the two versions and see what the memory usage from the compiler says? |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Tue Aug 11, 2009 11:13 am |
|
|
Yes, of course....
individually it makes no difference but the compiler only reports in % used, not bytes so I can't see if there is some minor difference that may add up over the whole of the project.
I could rewrite all my code to find out, but that's a lot of work if in the end it makes no difference at all.
So I suppose I wasn't clear and I should rephrase: "in theory does it make any difference in compiled memory usage if variables are declared globally or if they are passed from function to function and handled locally?"
The latter case I prefer in terms of readibility and manageability, but I can't help but to wonder if using globals is more efficient. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Aug 11, 2009 11:26 am |
|
|
I worked with an outside consultant who thinks that using globals are more efficient. I personally have not investigated the issue. Maybe you can look at the exact size of the hex file created by the two versions as that will give it to you in bytes and not percent. |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Tue Aug 11, 2009 11:28 am |
|
|
Oh, right! Hey, now that's the kind of obvious answer I needed!
Not sure why I didn't think of that, but thank you. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Aug 11, 2009 11:34 am |
|
|
Please let us know what you find as I am curious myself.
I would tend to think that using globals are more efficient since there are less variables for the variables to keep track of. |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Tue Aug 11, 2009 11:56 am |
|
|
And the winner by a VERY slim margin.... LOCALS
That's counterintuitive to me, but I guess the numbers don't lie.
I took one of my programs and swapped one function inside from using locals to using globals (similar to that described above) careful not to eliminate anything or add anything unrelated. The result was the hex file using globals came to 5,438 bytes while the file using locals came to 5,360 bytes
However, that's pretty close! I'd like to read others' comments on the subject but from that little experiment I'm happy to stick with the way I was organizing my code. I still don't have a solution for my space problems though! Oh well, that's my problem. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 11, 2009 11:59 am |
|
|
Your two test programs are not basically equivalent, so there's no easy
way to compare the two. Also, one of them doesn't compile. Ideally,
they would be identical, except in the method of returning the function
results (i.e., retval or global)
But, you can get the exact amount of ROM and RAM used. It's given
at the top of the .LST file. Here's the results for the one that compiles:
Code: |
CCS PCH C Compiler, Version 4.095, xxxxx 11-Aug-09 10:42
Filename: PCH_Test.lst
ROM used: 506 bytes (3%)
Largest free fragment is 15878
RAM used: 10 (1%) at main() level
21 (3%) worst case
Stack: 2 locations
float x = 2.03; //declared globally
void test_function()
{
x = x * 2;
}
void main()
{
while (1)
{
test_function();
if (x>10)
{
//whatever
}
}
} |
|
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Tue Aug 11, 2009 12:04 pm |
|
|
Thanks, I didn't know that.
I typed the silly examples here only - I never tried them in the compiler. Although I can't immediately see what you mean by "not basically equivalent" - it doesn't matter really, my point was generic and not to do with that particular example.
Cheers. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 11, 2009 12:16 pm |
|
|
I meant, start with a test program that returns a result by using a
"return" statement. Compile it, and save the .LST file in a safe place.
Now modify that program. Comment out the "return" statement,
and add a global variable. Edit the function to put the result in that
global variable. Compile. Then compare the two .LST files.
What I meant was, make the differences between the two test programs
be as little as possible. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 11, 2009 12:47 pm |
|
|
In the (older versions of the) Microchip and High Tech compilers you had to manually assign the memory, in CCS you just declare a local variable and have the compiler allocate memory for it. This makes the CCS compiler 'special' because it is very efficient in re-using the memory for local variables.
Knowing this it is very hard to predict which method will give a smaller memoy print. It will depend on the type and number of variables used, the parameters the return type and the number of times the function is called. You would have to test for each function.
Personally I don't like global variables as they make source code much harder to read, I like the 'encapsulation' paradigm of local variables. When used with care you should be able to save a few bytes by using global variables but often there is other low hanging fruit that's easier to pick.
Study the list file (*.lst) to learn which coding concepts take a lot of memory. For example array indexing is using a relative large number of instructions.
Last edited by ckielstra on Tue Aug 11, 2009 12:53 pm; edited 1 time in total |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Tue Aug 11, 2009 12:50 pm |
|
|
very good, thank you! |
|
|
|