Martin Berriman
Joined: 08 Dec 2005 Posts: 66 Location: UK
|
|
Posted: Sun Mar 09, 2008 6:47 am |
|
|
Thought I would post on the outcome of my optimisation efforts.
Having followed the suggestions from Ttelmah and ckielstra, I managed to save over 20% code space! Particular thanks to ckielstra for all his help
For anyone else who might be trying to optimise code, here are the things that I found worked best (note this was for an 18F device and things might be different when optimising 16F devices):
1) Search for functions that return values - do you really need that return value? - I found that quite often I had functions returning bools however in many places I was not using the return value therefore with a bit of tweaking, I managed to eliminate the return and saved quite a significant amount of code.
2) Examine each switch statement. It is tedious but test each statement with and without a default statement (ie add default: break;). In some cases it saves code and in others, removing the default case saves code. A surprising amount of space was saved this way (sounds like the compiler could do with some improvement here).
3) Try to minimise use of sprintf. I have to pass strings to my LCD for display however:
Code: | buf[0] = 'H';
buf[1] = 'e';
buf[2] = 'l';
buf[3] = 'l';
buf[4] = 'o';
buf[5] = '0' + val;
buf[6] = '\0';
takes a lot less code than:
sprintf(&buf[0], "Hello%u", val);
|
I hope this helps someone else |
|