Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

2D array vs bootyfab

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

Bascom Member



Joined: 10 Feb 2014
Posts: 74
Location: Melbourne

australia.gif
PostPosted: Sun Mar 24, 2024 10:15 am    Post subject: 2D array vs bootyfab Reply with quote

Hi guys, I've been working on this...

Is there any meaningful advantage to this method compared to using a standard 2d array in regards to execution speed?
it's for a 30 x 30 table

Code:

   select case HeadingDeviation '/(case value = x axis)

      case 1:  Yaxis(0+accel) = balancepoint
      case 2:  Yaxis(30+accel) = balancepoint
      case 3:  Yaxis(60+accel) = balancepoint
      case 4:  Yaxis(90+accel) = balancepoint
      case 5:  Yaxis(120+accel) = balancepoint
      case 6:  Yaxis(150+accel) = balancepoint
      case 7:  Yaxis(180+accel) = balancepoint
      case 8:  Yaxis(210+accel) = balancepoint
      case 9:  Yaxis(240+accel) = balancepoint
      case 10: Yaxis(270+accel) = balancepoint
      case 11: Yaxis(300+accel) = balancepoint
      case 12: Yaxis(330+accel) = balancepoint
      case 13: Yaxis(360+accel) = balancepoint
      case 14: Yaxis(390+accel) = balancepoint
      case 15: Yaxis(420+accel) = balancepoint
      case 16: Yaxis(450+accel) = balancepoint
      case 17: Yaxis(480+accel) = balancepoint
      case 18: Yaxis(510+accel) = balancepoint
      case 19: Yaxis(540+accel) = balancepoint
      case 20: Yaxis(570+accel) = balancepoint
      case 21: Yaxis(600+accel) = balancepoint
      case 22: Yaxis(630+accel) = balancepoint
      case 23: Yaxis(660+accel) = balancepoint
      case 24: Yaxis(690+accel) = balancepoint
      case 25: Yaxis(720+accel) = balancepoint
      case 26: Yaxis(750+accel) = balancepoint
      case 27: Yaxis(780+accel) = balancepoint
      case 28: Yaxis(810+accel) = balancepoint
      case 29: Yaxis(840+accel) = balancepoint
      case 30: Yaxis(870+accel) = balancepoint

   end select


(BASCOM-AVR version : 2.0.8.6 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Sun Mar 24, 2024 10:29 am    Post subject: Reply with quote

i do not see the point of the select case, you can calculate the index right?
if you do not want to multiply you can also use a DATA table with the offset. then use lookup() get the index and add accel.
a huge select case with predictable values is not a good idea imo.
in worse case all values are checked before the proper is found. so that can take a long time.

you can easily test it with the sim.

_________________
Mark
Back to top
View user's profile Visit poster's website
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 74
Location: Melbourne

australia.gif
PostPosted: Sun Mar 24, 2024 11:11 am    Post subject: Reply with quote

albertsm wrote:
i do not see the point of the select case, you can calculate the index right?
if you do not want to multiply you can also use a DATA table with the offset. then use lookup() get the index and add accel.
a huge select case with predictable values is not a good idea imo.
in worse case all values are checked before the proper is found. so that can take a long time.

you can easily test it with the sim.



Thanks for the reply.
I'd have thought select case would be the faster than a data table, but I assume a lot of incorrect stuff Smile


I wrote the above not realising bascom did multiple dimensional arrays. It's ugly code and if it's not significantly faster I'll change to a proper 2d array.
Sorry to say - Ive never used the sim.


this below is from the help files, and is what got me second guessing about going down the proper 2d road.

'While using multiple indexes might be a nice feature, it comes with a penalty : the processor need to calculate the address in memory based on the indexes. The more indexes you add, the more calculations/code is required.'
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Sun Mar 24, 2024 11:27 am    Post subject: Reply with quote

yes multi dim requires more code. for an array(a,b,c,d) all the indexes must be multiplied with the size and added to get the proper memory location.
_________________
Mark
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Mar 24, 2024 6:17 pm    Post subject: Re: 2D array vs bootyfab Reply with quote

snipsnip wrote:
Is there any meaningful advantage to this method compared to using a standard 2d array in regards to execution speed?

Your unrolled code executes in the statistical mean slower than calculating the index in a one-dimensional array and both execute faster than a two-dimensional one.
Code:
Dim HeadingDeviation As Byte
Dim accel As Byte
Dim balancepoint As Byte
Dim Yaxis(900) As Byte

Dim testpos As Byte

   select case HeadingDeviation   ' 21 to 163 cycles
      case 1:  Yaxis(0+accel) = balancepoint   ' 21
' ...
      case 15: Yaxis(420+accel) = balancepoint   ' 91
' ...
      case 30: Yaxis(870+accel) = balancepoint   ' 163
   end select

Dim idx As Word

idx = HeadingDeviation - 1   ' 81 cycles
idx = idx * 30
idx = idx + accel
Yaxis(idx) = balancepoint

Dim Yax(30, 30) As Byte

Yax(HeadingDeviation, accel) = balancepoint  ' 232 cycles
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