Noob Global Scope
BlitzMax Forums/BlitzMax Beginners Area/Noob Global Scope
| ||
Hi clever people Ive been looking though the code archives and tutorials and Ive noticed something. Sometimes variables at Global scope are declared as "Local" Now thinking about this logicaly I could assume that Globals declared Variable are "Static" across all the instances of the program that are running, however Im nearly sure that BMax doesnt work like that. Or I could assume that in this situation #Label Global x:int =0 X:+1 Print x Goto LabelThe decleration of X is static, and so the output would be 1,2,3,4,5,6 etc whereas #Label Local x:int =0 X:+1 Print x Goto LabelX would be reset to 0, so the output would be 1,1,1,1,1 Which they do ;) Edit: GLobal 1,2,3,4,5 Local 1,1,1,1,1,1 So Ive seen for myself that there is a differnce between Local at Global Scope, and Global at Global Scope. (A difference that Ive never seen before) However, although Its useful, I dont really use this feature (Well I do in Functions and the like, but not at global scope Cos I didnt know it was there), so Im willing to Local or Global Global scope variable. But are there any other differences that Im unaware of? Like ,speed or Optimisation etc. |
| ||
I'd be interested in a speed test, myself. I'm not sure but I wonder if global variables aren't stored in a different address space than local variables, which are stored...on the stack, aren't they? The difference in speed between locals and globals in most function calls can be drastic (in a bottleneck function, anyhow.) |
| ||
When you define the global, if you go back and redefine it you dump the old X and make a new one which you set to 0. So yes you get 1,1,1,1,1 Locals are faster than Globals since globals are stored in memory whereas locals can be CPU registers. Use as many locals as you can. Normally you should define your globals near the start of the program and then your #Label routine would only be used to make modifications to its value, not to define it as a variable. I usually put globals at the start of my program. |
| ||
When you define the global, if you go back and redefine it you dump the old X and make a new one which you set to 0. So yes you get 1,1,1,1,1 Err, no you dont, Please run the code. (Or read what I posted) If its a Global it isnt redefined, if its a local it is |
| ||
The entire global scope ends up in a function, so the same rules apply. |
| ||
oh |