BASIC

Author Topic: GLBasic and the use of arrays  (Read 5963 times)

Offline Ruidesco

  • Mr. Polyvector
  • ***
  • Posts: 236
    • View Profile
GLBasic and the use of arrays
« on: 2011-Nov-02 »
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]

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4168
    • View Profile
    • Portfolio
Re: GLBasic and the use of arrays
« Reply #1 on: 2011-Nov-02 »
strange, it crashes as I press f5

Offline fuzzy70

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 828
  • Look left, Look right, LOOK OUT!!
    • View Profile
Re: GLBasic and the use of arrays
« Reply #2 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
« Last Edit: 2011-Nov-02 by fuzzy70 »
"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)

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: GLBasic and the use of arrays
« Reply #3 on: 2011-Dec-14 »
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

  • Guest
Re: GLBasic and the use of arrays
« Reply #4 on: 2011-Dec-14 »
Might be worth adding in some options to enable/disable that

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 929
  • KodeSource
    • View Profile
    • KodeSource
Re: GLBasic and the use of arrays
« Reply #5 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]

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10682
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: GLBasic and the use of arrays
« Reply #6 on: 2011-Dec-15 »
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.

Offline Crivens

  • Prof. Inline
  • *****
  • Posts: 913
    • View Profile
Re: GLBasic and the use of arrays
« Reply #7 on: 2011-Dec-15 »
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.

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: GLBasic and the use of arrays
« Reply #8 on: 2011-Dec-15 »
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.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2580
    • View Profile
Re: GLBasic and the use of arrays
« Reply #9 on: 2011-Dec-16 »
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)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10682
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: GLBasic and the use of arrays
« Reply #10 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.

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4141
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: GLBasic and the use of arrays
« Reply #11 on: 2011-Dec-16 »
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.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2580
    • View Profile
Re: GLBasic and the use of arrays
« Reply #12 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)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10682
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: GLBasic and the use of arrays
« Reply #13 on: 2011-Dec-16 »
I'm using some own stuff that works like std::vector. But a bit more cleverness for the REDIM and such.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5006
  • O Rly?
    • View Profile
Re: GLBasic and the use of arrays
« Reply #14 on: 2012-Jan-08 »
DGArray wins!
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard