View previous topic :: View next topic |
Author |
Message |
Sebastian.Heyn
Joined: 13 Apr 2005 Posts: 84
|
Posted: Tue Apr 07, 2015 2:09 pm Post subject: [solved] Best BASCOM way to handle these many IFs |
|
|
Hi
I am creating a kind of lookup table, but I dont like this below method, so my question is howto do this more elegant?
if byte3=&h00 then f_str=f_str+"bla bla"
if byte3=&h01 then f_str=f_str+"another thing"
.
.
.
.
problem is that I have many values - and sometimes there are gaps
like 00,01,34,36,8b
I though about putting this to the flash data and use read to read the data, but that would slow down my program significantly.
Any ideas?
(BASCOM-AVR version : 2.0.7.8 )
Last edited by Sebastian.Heyn on Tue Apr 14, 2015 7:28 am; edited 1 time in total |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Apr 08, 2015 12:05 am Post subject: |
|
|
Look at the SELECT CASE statement. This handles that type of construction pretty well. So does LOOKUP into an array in FLASH. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
Sebastian.Heyn
Joined: 13 Apr 2005 Posts: 84
|
Posted: Wed Apr 08, 2015 6:58 am Post subject: |
|
|
mmhh... thanks i will check out both. |
|
Back to top |
|
|
Sebastian.Heyn
Joined: 13 Apr 2005 Posts: 84
|
Posted: Wed Apr 08, 2015 7:34 am Post subject: |
|
|
OK.. following test:
48 IF byte3=..... then .... instructions vs. 48 Case...
The byte3 value was simulated in the middle (20ish somewhere) of all the options
If then : 484 cycles -> checks all ifs, even if the value was found
Case: 270 cycles -> stops after the value was found
Each "if" takes 7 cycles
each "else" takes 5 cycles
I guess CASE is the clear favourite in this situation.
lookupstr is a nice function, the code looks much cleaner.
However I need speed so it is not the way to go for me.
I will keep this function in mind in case I need 2-way translation
Code: |
f_str=lookupstr(0,t_data) '133 cycles
print f_str
f_str=lookupstr(1,t_data) '137 cycles
print f_str
f_str=lookupstr(2,t_data) '228 cycles
print f_str
f_str=lookupstr(25,t_data) '689 cycles
print f_str
t_data:
data "Hello","","this","","","","","","",""
data "","","","","","","","","",""
data "","","","","","26","","","",""
|
|
|
Back to top |
|
|
cored
Joined: 13 Aug 2008 Posts: 10 Location: Tallinn
|
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5905 Location: Holland
|
Posted: Wed Apr 08, 2015 9:00 am Post subject: |
|
|
Quote: | If then : 484 cycles -> checks all ifs, even if the value was found |
you do know that there is the elseif statement right?
Quote: | dim bb as Byte
bb=2
if bb=1 then
print "1"
elseif bb=2 then
print "2"
elseif bb=3 then
print "3"
elseif bb=4 then
print "4"
end if
|
it will evaluate till the condition is true, then it will continue after the end if.
from the database like suggested you could do this : make a crc32 of each string and store that. possible combined with an index _________________ Mark |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 970
|
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Apr 09, 2015 12:30 am Post subject: |
|
|
If the variable you are looking for is a byte, then there are only 256 possible values. So if you had a table indexed by the byte value, the you can go directly to the indexed value without using a search. That is by far the fastest way. Even if your table has gaps, just fill the gap with a null or some other placeholder. If your target result has to be a variable length string, you can still do it with an indexed table, by using two steps. First make a table of words, indexed by the byte value, then make a second table of addresses to the start of each string, indexed by the word value. Then in two steps, you can recover the address of the string, without doing any searching. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
PeterM
Joined: 09 Jun 2008 Posts: 118
|
Posted: Thu Apr 09, 2015 10:08 am Post subject: |
|
|
There is also ON VALUE you can try _________________ Peter |
|
Back to top |
|
|
Sebastian.Heyn
Joined: 13 Apr 2005 Posts: 84
|
Posted: Tue Apr 14, 2015 7:28 am Post subject: |
|
|
@AlbertSM:
Actually YES, but I never thought about using it |
|
Back to top |
|
|
|
|
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 You cannot attach files in this forum You cannot download files in this forum
|
|