useing Global

BlitzMax Forums/BlitzMax Beginners Area/useing Global

Ian Lett(Posted 2011) [#1]
This may sound like a daft question, but is useing Global as oposed to Local causing any performance loss or is it ok to use Globals genraly and just use local in a few places?

regards


Htbaa(Posted 2011) [#2]
You should use Local as much as possible. Anything Global will always be available during the entire runtime of a program, and taking up memory. Everything Local will me collected by the garbage collector when out of scope.

It's best practice anyway to let as little as possible be dependent of Global (state).


H&K(Posted 2011) [#3]
Ok, lets clear something up.

Any performaance loss or gain by using Global will NOT create or clear any Bottleneck in your program.

Its possible to make it the bottle neck, but you would have to go out of your way to do so.

Having said that, try to have as few true Global as possible, one method is to have a "Game Singleton;- Type", this would be a type you create one instance of, and it has global fields, eg (Taking screen width)

Global screenwidth:int = 1024
or
Game.Screen.Width = 1024

(Ok so I made Screen dimensions a Vector pair lol)

The benefits of this multiply exponentially when you start you use predictive text ie BLIDE)


Czar Flavius(Posted 2011) [#4]
I agree with these posts. (Although I dislike the singleton design pattern) With global variables there are two seperate issues of concern.

The first is that global are very, very, very slightly slower than locals. However, unless you are using them for almost every variable, the performance impact is negligible. Don't even care about it.

However, global variables should be discouraged as they tend to indicate bad program design. This has nothing to do with performance, but about keeping your code tidy and organised. For example, you might make the player's position or lives a global variable. But what if you want to add two player mode? It's better to pass the player's details into a function as parameters (using a player Type for instance) rather than retreive the data from specific global variables inside the function. Then you can reuse the same function for any number of players - it's just a case of passing the right player type object to the function.

Things such as the current resolution, volume setting, etc, you can happily keep as global variables. I also find it can be handy to keep a global count of the number of certain objects in the same, to help debug and such.

Last edited 2011


CS_TBL(Posted 2011) [#5]
Apart from that,

A lot of things in a visual world have a width and a height, such as players, bullet sizes, game tiles, room dimensions, map dimensions etc. In the ideal case such variables are always 'width' and 'height', so that you never have to wonder, 3000 lines below the definition of such things what the names were again. But as there's only one width and height possible in your variable space, it'll form a problem. The alternative would be having types attached to the name, such as playerwidth, mapwidth, roomwidth, bulletwidth, tilewidth etc. but by then you should realize that types/classes have been invented for a reason: MyPlayer.width, MyMap.width, MyRoom.width, SomeBullet.width, Tile.width etc. So, in reality the number of variables (names) you'll be using with types is typically less than when using globals, making it easier for a programmer to deal with 'em. One can extend types with this idea, having a base type with 'width' and 'height', and have extended types with the rest of the details. Often I find these mechanisms a bit far stretched though, I do use it in an easy canvas system I've made though.

Last edited 2011


Ian Lett(Posted 2011) [#6]
thanks all, this has been realy usefull, i was trying to minimise the global use, as it does help to keep code neat and tidy.

regards
Ian


xcessive(Posted 2011) [#7]
On an unrelated topic, I accidentally read your name as Iam Leet for this entire thread.


ImaginaryHuman(Posted 2011) [#8]
Don't keep creating the same variables inside a loop, either. Create it once outside the loop as a Local and then use it in the loop.

Locals potentially go in CPU registers semi-permanently and can be much faster. Globals are always stored in main memory which is slower. Use as many locals as you can where possible.


H&K(Posted 2011) [#9]
@ Imaginary

Do you happen to know where Global Fields used as loop indexes go?

Eg
Type aType

Global LoopIndex

MEthod AMethod()

For LoopIndex= 1 to 20
next
end method: End Type
As a global field Im looking at one creation, and possibly being put on a register when in the loop. Where as for a local Im looking at creation every call, and GC


Czar Flavius(Posted 2011) [#10]
For Local LoopIndex= 1 to 20
Next

Globals and fields are never put on registers, only locals. Only objects are garbage collected, not integers or floats. A local created inside the for loop or nearby scope is the fastest way.

Last edited 2011


Yan(Posted 2011) [#11]
Strict


Czar Flavius(Posted 2011) [#12]
Strict is a good idea in general, but I don't see how it relates to deciding whether to use global or local variables?


H&K(Posted 2011) [#13]
@Czar

Thanks.