Local vs Global - General Discussion and Assistance - CHDK Forum

Local vs Global

  • 5 Replies
  • 258 Views
Local vs Global
« on: 24 / January / 2020, 04:51:18 »
Advertisements
Once again, to help me become a better CHDK scripter, I would welcome one of the CHDK gurus throwing some light on the use of global vs locally declared variable.


I’m fully aware of the benefit of using locally declared variables, eg in functions or loops etc.


What I’m not so sure about is the potential CHDK advantage of explicitly declaring all variables as local, at a script’s start up, vs using variables as globals, ie ‘declaring’ them as they appear in the script.


I believe there is a speed advantage to using locals, but don’t know if this has real value in my scripts? Plus, there may be other benefits in using locals, eg stability in the script or the CHDK backend.


As I say, I’d welcome some education  ;)
« Last Edit: 24 / January / 2020, 05:16:14 by pigeonhill »

Re: Local vs Global
« Reply #1 on: 24 / January / 2020, 10:33:22 »
AFAIK declaring Local variables within functions is all about scope control and code quality.

Declaring Local variables at the start of a script is a hold over from bigger systems with multiple source files where you don't want variable names to collide.  Not too useful with the single source file CHDK Lua implementation.

As far as any performance differences go, even if there are any theoritical ones,  I don't believe it will have any measurable effect in any CHDK application.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: Local vs Global
« Reply #2 on: 24 / January / 2020, 10:37:32 »
@waterwingz


Thanks for the insight.


I’ve seen various CHDK scripts using local in the main area.


Great to know I needn’t be worried about local vs global.


Cheers


Garry

Re: Local vs Global
« Reply #3 on: 24 / January / 2020, 10:47:56 »


@waterwingz
Great to know I needn’t be worried about local vs global.
I wasn't suggesting that.  Judicious use of local variables within functions will greatly increase code quality of complex scripts.  It's just not going to speed script execution times.
Ported :   A1200    SD940   G10    Powershot N    G16


Re: Local vs Global
« Reply #4 on: 24 / January / 2020, 11:02:15 »
Got it. Thanks

*

Offline reyalp

  • ******
  • 12544
Re: Local vs Global
« Reply #5 on: 24 / January / 2020, 21:38:56 »
I believe there is a speed advantage to using locals, but don’t know if this has real value in my scripts? Plus, there may be other benefits in using locals, eg stability in the script or the CHDK backend.
As waterwingz said, in normal cases the performance differences will be totally irrelevant. Measurements: https://chdk.setepontos.com/index.php?topic=12451.msg123416#msg123416

The only place I'd worry about this is if you are running loops with thousands of iterations. It is worthwhile if you doing something processing a large number of raw pixel values.

There should be no difference in stability, locals are probably marginally more memory efficient. 

If you are writing modules that will be loaded with "require" it's normally a best practice to use local at the top level scope of the module.
Don't forget what the H stands for.

 

Related Topics