Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

[solved] Best BASCOM way to handle these many IFs

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Sebastian.Heyn

Bascom Member



Joined: 13 Apr 2005
Posts: 84

PostPosted: Tue Apr 07, 2015 2:09 pm    Post subject: [solved] Best BASCOM way to handle these many IFs Reply with quote

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
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Wed Apr 08, 2015 12:05 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Sebastian.Heyn

Bascom Member



Joined: 13 Apr 2005
Posts: 84

PostPosted: Wed Apr 08, 2015 6:58 am    Post subject: Reply with quote

mmhh... thanks i will check out both.
Back to top
View user's profile
Sebastian.Heyn

Bascom Member



Joined: 13 Apr 2005
Posts: 84

PostPosted: Wed Apr 08, 2015 7:34 am    Post subject: Reply with quote

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
View user's profile
cored

Bascom Member



Joined: 13 Aug 2008
Posts: 10
Location: Tallinn

estonia.gif
PostPosted: Wed Apr 08, 2015 8:49 am    Post subject: Reply with quote

If you have a huge amount of strings, some kind of indexing would help http://en.wikipedia.org/wiki/Database_index
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4975
Location: Holland

blank.gif
PostPosted: Wed Apr 08, 2015 9:00 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 674

poland.gif
PostPosted: Wed Apr 08, 2015 7:48 pm    Post subject: Reply with quote

Quote:
lookupstr is a nice function, the code looks much cleaner.


If You have code where in multiple places must be printed the same thing/string then You can choose constans.
This constans can be stored in one place in code and qiuck translated to another language for example.
You can multiple times use them in code and compiler store only address of them Smile I think so Very Happy

Code:

Const Sign = ">"
Const Str_1 = "String example 1"
Const Str_2 = "String exapmle 2"
Const Error1 = "Error 1WIRE"
Const Error2 = "Error I2C"

   Lcd Error1  
   Lcd Error1  'can be used many times
   Lcd Str_2

 String_s = Str_1
 



I hope every idea can be helpfull Smile
Back to top
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Thu Apr 09, 2015 12:30 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
PeterM

Bascom Member



Joined: 09 Jun 2008
Posts: 118

australia.gif
PostPosted: Thu Apr 09, 2015 10:08 am    Post subject: Reply with quote

There is also ON VALUE you can try
_________________
Peter
Back to top
View user's profile
Sebastian.Heyn

Bascom Member



Joined: 13 Apr 2005
Posts: 84

PostPosted: Tue Apr 14, 2015 7:28 am    Post subject: Reply with quote

@AlbertSM:

Actually YES, but I never thought about using it Wink
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
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
You cannot attach files in this forum
You cannot download files in this forum