CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Optimising code
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Martin Berriman



Joined: 08 Dec 2005
Posts: 66
Location: UK

View user's profile Send private message

PostPosted: Sun Mar 09, 2008 6:47 am     Reply with quote

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! Shocked Particular thanks to ckielstra for all his help Very Happy

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 Cool
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group