View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sun Mar 29, 2015 4:36 am Post subject: What is better - a Dimensioned variable or a local variable? |
|
|
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 |
|
|
snipsnip
Joined: 10 Feb 2014 Posts: 72 Location: Melbourne
|
Posted: Sun Mar 29, 2015 8:59 am Post subject: |
|
|
I'm still learning all this stuff - but anyway here's my 2 cents...
It all depends
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 |
|
|
EDC
Joined: 26 Mar 2014 Posts: 970
|
Posted: Sun Mar 29, 2015 11:17 am Post subject: |
|
|
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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sun Mar 29, 2015 5:53 pm Post subject: |
|
|
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 |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Mon Mar 30, 2015 9:36 am Post subject: |
|
|
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 |
|
Back to top |
|
|
|