I've been reading and learning about optimizing your code. The project I am working on is somewhat large consisting of 400 lines of code and using 66% (20,318 bytes) of program storage and 56% (1,164 bytes) of dynamic storage. (AVR 328p)
I have all my print/write statements using the F() macro to keep them in Flash, I have minimized my global variables to just what is needed to be global and have when possible made the globals byte type or limited char arrays of a set length. Where possible I have used local variables in functions the largest function having 212 bytes of variable space.
My biggest question is how much space should I leave available in Program space and dynamic memory? Is there a rule of thumb on this or is it specific to the program?
The other question is what advice would you give to optimize the space/usage?
Thank you
My biggest question is how much space should I leave available in Program space and dynamic memory?
You can fill program space. How much SRAM needs to be available depends on how many levels of function calls are needed, and how many (and what type of) values are being passed to the functions. The general rule of thumb is that when the program stops working reliably, you have used too much SRAM.
Just don't use the recursive algorithms that your CS professors in college seemed really fond of.
I don't think that local variables use less space than global variables. Actually, with careful planning, re-use of global variables for temporary counters, indexes and so on, could result in a smaller allocation than if they were declared locally. But there is a danger of unplanned collisions, which is why the machinery of scope was introduced. Personally, I value code integrity and ease of use more highly than memory allocation, so I use a lot of locals. It is also possible that the compiler optimizer is capable of automatically reusing the memory space previously belonging to locals from another procedure where the values are discarded after exiting that procedure. In that case it would make no difference.
With what the hardware costs these days, doing software optimization seems to me like a complete waste of time. I buy Due boards (83MHz Arm7 32-bit processor, 512K FLASH, 96K RAM) for $14 each in one-sies. If that's not enough, for $35 I can buy a Raspberry Pi model B with a 4-core 700MHz processor, and more memory than I could possibly ever use. Unless the optimization takes less than 15 minutes, it's cheaper for me to buy a new board with more memory and/or horsepower.
Regards,
Ray L.
You can use the function at the bottom of this page : Arduino Playground - AvailableMemory
That is a much better indication than the number by the compiler.
When I don't use the String object, and only very small buffers on the stack (10 bytes or so), and not many function calls, then I have no problem to go up to 75% of ram.
aarg:
I don't think that local variables use less space than global variables. Actually, with careful planning, re-use of global variables for temporary counters, indexes and so on, could result in a smaller allocation than if they were declared locally.
I've found significant savings by moving code out of setup() and loop() into smaller functions, each with its own local variables that had previously been local within setup or loop. These weren't really sharable variables to begin with so having them all in one place just increased the maximum extent of the stack. In separate smaller functions they essentially share the same stack area.
aarg:
I don't think that local variables use less space than global variables.
On an AVR processor? With code produced by avr-gcc?
Bah. The first few local variables are put exclusively in registers; they consumer zero SRAM.
RayLivingston:
With what the hardware costs these days, doing software optimization seems to me like a complete waste of time.
Ray, is this still true when size and power consumption are important? I mean, is there ever a reason to use a lesser processor like the 328?
KeithRB:
Just don't use the recursive algorithms that your CS professors in college seemed really fond of.
Never did the whole CS thing. However, my first computer was a Timex Sinclair Which was not much bigger than these 328p's lol Z80a processor and 2k of ram. Though it only ran at 3MHz. Not a lot of space when you are programing in basic. lol
I did not know that, thank you
RayLivingston:
With what the hardware costs these days, doing software optimization seems to me like a complete waste of time. I buy Due boards (83MHz Arm7 32-bit processor, 512K FLASH, 96K RAM) for $14 each in one-sies. If that's not enough, for $35 I can buy a Raspberry Pi model B with a 4-core 700MHz processor, and more memory than I could possibly ever use. Unless the optimization takes less than 15 minutes, it's cheaper for me to buy a new board with more memory and/or horsepower.
Regards,
Ray L.
I have seen this said a few times but there are size limitations to many of my projects that do not allow even an R3 board. I have several Pi's and they are great but for some jobs they are just too big (physical size)
There is also something to be said about optimizing code and making it as small as possible.
I see what you mean. I also now remember the business about local variables using stack storage, which is disposable (hence reusable). It is obviously preferable for that reason. But what if the programmer simply doesn't use locals? Because I think that was the original discussion, it was local vs. global. In that case, wouldn't the compiler also put the global variables that it is currently using into registers?
Registers are used to manipulate global / static data. The storage for global / static data is always SRAM.
RayLivingston:
With what the hardware costs these days, doing software optimization seems to me like a complete waste of time. I buy Due boards (83MHz Arm7 32-bit processor, 512K FLASH, 96K RAM) for $14 each in one-sies. If that's not enough, for $35 I can buy a Raspberry Pi model B with a 4-core 700MHz processor, and more memory than I could possibly ever use. Unless the optimization takes less than 15 minutes, it's cheaper for me to buy a new board with more memory and/or horsepower.
Regards,
Ray L.
But it's not always about cost. It can be about size and power consumption. Especially if it has to be battery powered.
CyberLink1, you started this topic with a question about 66% code and 56% ram.
That is no problem at all.
If you want to add more in to your sketch, you can show us your sketch, and we can shrink it. It is even possible to add custom compiler flags. For example the -flto option will shrink a sketch by 5% to 20%.
Did you know that the 'F()' is for Serial.println(), but the 'PSTR()' is for strcpy_F() and sprintf_F().
Using many different library functions will link all those functions in. Sometimes code can be shrinked by using only a few library functions, even if some extra code is needed.
Sometimes I remove all the floating point operations, that saves also a few bytes.