Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

What is better - a Dimensioned variable or a local variable?

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

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Sun Mar 29, 2015 4:36 am    Post subject: What is better - a Dimensioned variable or a local variable? Reply with quote

Hello all,

Question - what is better, a Dimensioned variable (Global) or a Local variable? There is probably a lot of opinions on this and it likely depends on the circumstances. So to better define the question allow me to restate.

When a sub or function is called a LOT what is the time difference between using a variable that is already dimensioned, and one whose memory space must have an allocation created? Is it a waste of time to do a Local since in my cases these subs/functions are used virtually continuously? Also in my specific case, there are a lot of events firing which may add to the overhead - not really sure about all this - not my strongest point!

Opinions and feedback are appreciated!

Thanks,
Tim

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 66
Location: Melbourne

australia.gif
PostPosted: Sun Mar 29, 2015 8:59 am    Post subject: Reply with quote

I'm still learning all this stuff - but anyway here's my 2 cents...

It all depends Laughing

If you're low on memory locals will help.
globals are faster, but a nightmare to keep track of.

what I've just started doing is to use locals everywhere when developing the code. Make every function / sub stand alone with no side affects

once all the functions are done, work on the structure - once you get to this bit you'll know what needs to be converted to global, what subs / functions should be unpacked / put inline (gosub) etc (save the standalone functions so they can be used again elsewhere!)

But yeah, I don't think the time required to pass values to a sub is a big deal, shitty code / structure will be make the speed issue suck heaps more.

cheers
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Sun Mar 29, 2015 11:17 am    Post subject: Reply with quote

I was curious and I made a short test in the simulator. Create a local variable costing 15 cycles even if you do not use it. I forgot to remove the Local and the result was the same for the declared and local.
Code:
$regfile "m8def.dat"
$sim

Dim Dimmed_var As Word
Const Value = 65535

Config Submode = New

'sub for testing cycles
'----------------------
 Sub Local_test

'  Local Local_var As Word                                  '<creating takes 15cycles
'  Local_var = Value

   Dimmed_var = Value                                       '<breakpoint

 End Sub
'----------------------


 Call Local_test

End
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Sun Mar 29, 2015 5:53 pm    Post subject: Reply with quote

Thanks for the responses.

Doesn't that all change when you add in continuous, or seemingly so, interrupts that occur while performing some action. So the uC has to save its current data, pop over to the calling interupt, then go back and restore the previous data before resuming? It might do the same with a global but not really sure.

Tim
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 920
Location: France

france.gif
PostPosted: Mon Mar 30, 2015 9:36 am    Post subject: Reply with quote

good question Tim
but sometime another question must be asked: is that I have enough ram to use all these global variables?

compromise is the art of micro-controller

jp Wink
Back to top
View user's profile Visit poster's website
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