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 support@ccsinfo.com

Speed limitations by SWITCH CASE statement ! Desperate

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

Speed limitations by SWITCH CASE statement ! Desperate
PostPosted: Thu Sep 15, 2005 6:19 am     Reply with quote

Hi everyone,

I am coming across a problem about speed.

I am working with a system in which there are a couple of PIC18F8722. One of them is the CONTROL PIC and another is GRAPHIC PIC.


There are menus and graphic drawings on the LCD connected to the GRAPHIC PIC. Initially, to build menus on LCD, I send string arrays(you can call them TEXT TEMPLATES) from CONTROL PIC to GRAPHIC PIC. There is a function in the GRAPHIC PIC that particularly displays text on the LCD and each time a string array is received by the GRAPHIC PIC this function 'LCD_Text' is called to display this text.


However, I finally ended up too many string arrays in the CONTROL PIC which is so big that it's reaching the memory capacity. So then I decided to move all these string arrays to the GRAPHIC and label them virtually with a number. So every time I want to display a specific text, I just simply send the corresponding label number from the CONTROL PIC to the GRAPHIC PIC. There is a large large SWITCH CASE statement in the GRAPHIC PIC which fetches a specific string by looking at the label.


The problem then happend! The speed is significantly slowed down!

It appears to me that
1. a SWITCH CASE statment assesses all the codes under all the 'case's before executing a specific section of code which takes much time ?
2. Though there is only one function "LCD_Text" with different parameters under each 'case', while the SWITCH CASE statement is examining all 'case's, it's accessing all "LCD_Text"s and it's the overheand of all the function calls that slow down the code?

These are my own explanations why the code slows down.

Please could anybody help me find out why my code slows down after I moved all the string arrays from CONTROL PIC to GRAPHIC PIC and used a large SWITCH CASE statement in the GRAPHIC PIC?

If there's not much I can do, can somebody recommend a better way to access a certain string array in the GRAPHIC PIC by using the corresponding label number received from the CONTROL PIC?


Since the code is very big, I appologize not being able to attach it here


Thanks
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

Re: Speed limitations by SWITCH CASE statement ! Desperate
PostPosted: Thu Sep 15, 2005 7:53 am     Reply with quote

ye wrote:

It appears to me that
1. a SWITCH CASE statment assesses all the codes under all the 'case's before executing a specific section of code which takes much time ?
2. Though there is only one function "LCD_Text" with different parameters under each 'case', while the SWITCH CASE statement is examining all 'case's, it's accessing all "LCD_Text"s and it's the overheand of all the function calls that slow down the code?


This seems REALLY odd! Are you using break statements in your switch cases? If not then multiple cases may be executed which could may things seem slow. Otherwise the switch code should just look at the case variable. Try to make that variable one byte if you can so the compares are very fast.
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 15, 2005 8:10 am     Reply with quote

There are several threads on this topic. Here are two of them:

Suggests removal of the 'default' case to improve performance:
http://www.ccsinfo.com/forum/viewtopic.php?t=20721

Suggests removal of #inline to improve performance:
http://www.ccsinfo.com/forum/viewtopic.php?t=21373
ye



Joined: 11 May 2005
Posts: 57
Location: london

View user's profile Send private message

Re: Speed limitations by SWITCH CASE statement ! Desperate
PostPosted: Thu Sep 15, 2005 8:48 am     Reply with quote

SherpaDoug wrote:
ye wrote:

It appears to me that
1. a SWITCH CASE statment assesses all the codes under all the 'case's before executing a specific section of code which takes much time ?
2. Though there is only one function "LCD_Text" with different parameters under each 'case', while the SWITCH CASE statement is examining all 'case's, it's accessing all "LCD_Text"s and it's the overheand of all the function calls that slow down the code?


This seems REALLY odd! Are you using break statements in your switch cases? If not then multiple cases may be executed which could may things seem slow. Otherwise the switch code should just look at the case variable. Try to make that variable one byte if you can so the compares are very fast.
Thanks for your comment. I don't think I would be too busy to forget using BREAKs! I don't have any doubt that the switch should only look at the case variable but if the result didn't end up so ODD, I wouldn't have made this ODD assumptions
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Sep 15, 2005 12:11 pm     Reply with quote

The switch case statement will use a jump table if you have enough cases. If you can you should use sequential case numbers starting with 1 and perform bounds checking before the select statement rather than using a default statement. Also be sure to use a byte for the case variable.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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