GLBasic and the use of arrays

Previous topic - Next topic

Ruidesco

Rather than a tutorial, these are just a few pieces of advice I've stumbled upon while coding the attached array performance example. This is probably something that the more seasoned GLBasic coders know, but newbies such as myself might find useful.

Dimensioning arrays:

  • Linear (1D) arrays are the fastest to use
  • It is slightly faster to use 2D arrays than simulating them through 1D arrays
  • It is considerably faster to simulate 3D or 4D arrays through 1D arrays than using real 3D or 4D arrays

Traversing arrays:

  • Traversing arrays linearly is the fastest way to work
  • To traverse both real multidimensional and simulated arrays the fastest way, nestle the dimension loops from highest to lowest dimension (as seen in the attached example)
  • Integer indexes (eg. i%) are faster to use than float indexes (eg. i#, i)

Additions are very much welcomed.

The result of the test in my own computer is attached as well.

[attachment deleted by admin]

erico

strange, it crashes as I press f5

fuzzy70

#2
Quote from: erico on 2011-Nov-02
strange, it crashes as I press f5

Same here  :blink:


Ahh, got it to work. Had to create a new project then paste/include the code in it then worked fine  =D
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Wampus

Are you guys just trying to compile the code straight from the GBAS file? It will crash if you do that. Open a new or old test project and paste the code into it.

Anyway, those are interesting results. Thanks for testing. I guess it may be worth simulating multidimensional arrays unless it starts to make your code harder to understand. I heard some great advice in a lecture by the creator of Braid. To paraphrase it, "Given the time restraints and the relative benefits of code optimisation for indie games you should only really optimise for getting things done."

MrTAToad

Might be worth adding in some options to enable/disable that

Slydog

Quote from: MrTAToad on 2011-Dec-14
Might be worth adding in some options to enable/disable that

Or a new command 'SAFEARRAY: state%=TRUE'

That way you have control over which sections of code get optimized.
Generally, for non time critical code, you want the safer method, but you may want to turn off array bounds checking during the main game loop when using large arrays or whatever.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

That would have to be a compile-time option. Do you want release builds not to check for array bounds? That will seriously cause trouble when you misbehave. You get errors and strange results at totally random spots then.
The checks are very quick. I don't think it's a big overhead.

Crivens

If it does give some speed ups then why not have a new taskbar option (like debug) that turns off/on final version compilation? This would then turn off the array bound checks but only when you are happy it's not going to fall over before hand with normal compilation. This could then be used for other similar things when they become apparent.

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

Wampus

The option would be nice but personally I don't think I'd ever use it. Inflicting users with obscure and practically untraceable errors in release builds is a horrible thought.

bigsofty

Well, having the option would be nice for me, I very rarely go out of bounds, having it permanently on feels a little wasteful. Only for a release build though, not debug.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

All C++ programs that use dynamic arrays have such a check (std::vector always does e.g.). It's not really an overhead.

Ian Price

Would it be possible then to add an error output message when our arrays do go out of bounds, rather than just terminating immediately? Even if it just states "Array out of bounds" or something (stating the actual array would be even better).
I came. I saw. I played.

bigsofty

Quote from: Kitty Hello on 2011-Dec-16
All C++ programs that use dynamic arrays have such a check (std::vector always does e.g.). It's not really an overhead.

Ah, I had assumed that std::vector was only used for certain situations within GLB, but when I come to think of it, there are very few static arrays within GLB.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

I'm using some own stuff that works like std::vector. But a bit more cleverness for the REDIM and such.

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard