View previous topic :: View next topic |
Author |
Message |
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
Speed limitations by SWITCH CASE statement ! Desperate |
Posted: Thu Sep 15, 2005 6:19 am |
|
|
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
|
Re: Speed limitations by SWITCH CASE statement ! Desperate |
Posted: Thu Sep 15, 2005 7:53 am |
|
|
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
|
|
|
ye
Joined: 11 May 2005 Posts: 57 Location: london
|
Re: Speed limitations by SWITCH CASE statement ! Desperate |
Posted: Thu Sep 15, 2005 8:48 am |
|
|
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
|
|
Posted: Thu Sep 15, 2005 12:11 pm |
|
|
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. |
|
|
|