Global - cost of each additional global

BlitzPlus Forums/BlitzPlus Beginners Area/Global - cost of each additional global

David Boudreau(Posted 2008) [#1]
What, exactly, is the cost of each additional Global variable in your source code? Is it only the space that one global would take up in regular RAM memory (or is it video memory/VRAM?)?

So let me make sure I understand this: An integer global costs the number of bytes it takes to represent a number between -2147483648 to +2147483647... which is... 4 bytes (as CreateBank doc says), and a 32-bit number. So somewhere in RAM or VRAM, for every Global integer declared, that is done at a cost of 4 bytes more when the program is running. Is that correct?

I try to make all variables that I can local, but often come to a point that I can't figure out how to do some algorithm without adding another global (I fully admit it's very possibly due to my own brain limits, not necessarily what is possible if I could think of a better algorithm!) but I am not sure what the downside or cost is each time I do that.

Instead of storing them in Type fields, would an array (always global when dimmed anyway) be faster? If not what are the trade-offs? Readability of code is definitely a plus (code is so much harder to read than it is to write!!).

These threads have good comments on this too:
http://www.blitzbasic.com/Community/posts.php?topic=33141#356613
http://www.blitzbasic.com/Community/posts.php?topic=76454#854999

Good to know that it does not tax the CPU during runtime as I read in one of those threads.


Matty(Posted 2008) [#2]
It's not worth worrying about.


xlsior(Posted 2008) [#3]
First of all, any variables are stored in regular RAM, not video memory.

global variables have a slightly higher overhead, in that they take up a tad more memory, and they cause a very slight slowdown in that it takes some extra time to pass the information over to function...
...but unless you have insane amounts of them, it's likely to be an almost imperceptible difference, and probably not worth worrying about.

Sure, if your code allows a local variable one might be preferable, but sometimes it's just a whole lot easier to use a global. They exist for a reason, after all.


Sauer(Posted 2008) [#4]
The real reason to limit your globals is because it often times makes code confusing and difficult to read. Its less of a problem in BASIC languages but in other languages, especially OOP languages like Python, globals are extremely frowned upon.

As for the speed of arrays vs. types, I (actually yesterday) was wondering the same thing so I wrote the code using both methods... noticed no difference in FPS. Although, my code, although simple, was running at 11 FPS or something awful like that so it might be something with my computer/code structure more than the types vs. arrays. Who knows.


David Boudreau(Posted 2008) [#5]
Thanks guys, good to know it's not a big deal. I don't think I have "insane" amounts (I doubt my globals would ever go over between 50 to 100) but I am working on a small project now (a small windowed app to run while other games run in the background), and I'm trying to take the time to do things right and pick up a better habit or two along the way.

Sauer, I think I remember reading years ago either in these forums or on blitzcoder about various tests to show when arrays or types were faster but I don't remember which and in what conditions as it wasn't too relevant to what I've done so far.

I'd like to do some of these tests myself but have been wondering lately what would be the best way to structure my code as a template base... like "What is the standard vanilla code template to start from in BlitzPlus?" to compare apples to apples, and also to have a good structure to start something quick when I get an idea and don't want to spend a lot of time setting up the basics each time (esp for windowed apps). In the doc and included examples, they all use different basic structures so when I read them I sometimes get confused between each sample's code structure vs. the purpose the code is demonstrating. Fundamental, basic things like putting your main loop code inside a $4001 timer or somewhere else.

I suppose the price of a template would be flexibility but even if I had several "code base templates" to select from I think that would be more productive. Anyone do this, or just simply copy and paste from their last project each time?


Sauer(Posted 2008) [#6]
Well I don't usually do windowed apps and the reason being that they do require more to initialize than a simple graphics window. I'm sure having a frame to begin with would decrease coding time considerably... having it open a window with a menu with an exit button or something.

Perhaps write your own function like MakeWindow(x,y,width,height) to do this...


Ross C(Posted 2009) [#7]
Types are slower than array, if you are creating new type object and destroying them frequently. Where as redimming an array is faster. I tried doing pathfinding with types and it was substantially faster using arrays.


Sauer(Posted 2009) [#8]
Hey thanks Ross C, that's very helpful information :)