speeding up mathematical operations?

Previous topic - Next topic


Hey everyone.

I´m working on a Binary puzzlegame and the puzzles are generated. Generating a puzzle at a playfield of 6x6 squares takes only 1-2 seconds or less.
But when I generate at 8x8 squares, It takes 20-30 seconds, which is a nogo  :'(

So I´m asking if there is generally a way to speedup mathematical operations.
Are there any tricks, or slow commands I should stay away from?
Should I use more integer variables to speed it up ?
Should I use repeat/until instead of while/wend?
Is there someting faster than mod command maybe?
Anything else I should be aware of?

Any help would be greatly appreciated.



Hard to say without playing around with the code and I can't really read the screenshot, too small text :-)

Most slow things I have seen in GL Basic has been when working with arrays that needs resizing so try to always create the arrays the size you need. But the arrays you use seems to be very small so should not be a problem. The arrays I was running that had problems with speed was more than a million lines and were pretty fast still.

There is a profiling function in the IDE that tells you what function is causing the slowest times, maybe there is a loop that is running more than you think? 20-30 seconds for such a small array sounds way too much unless you have a very old computer?


Since computers nowadays are ridiculously fast compared to 8bit, this issue should not be a problem unless you are dealing with really huge amounts of data or very complex calculations.
Anyways, what Moru said is the best answer, you can use the profiler to check that.


As GLB code is converted to C++ it is quite fast, only minor overhead is added, so all general optimization advice applies here to.
Use integers% rather than floats#, don't do redundant calculations, use arrays with static size - don't do DimPush DimDelete or use more sophisticated code to detect when array should be resized, use Alias when excessive using array element in loops, if can use 1-dimensional arrays instead 2/3d, SIN/COS can be replaced with pre-calculated values/faster versions. hm, that most that I can remember. And as said GLB is fast enough to calculate complex algorithms in realtime like hundreds of A* pathfinding, AABB collisions, random map generations, and so on, all depends on implementation and as other said it's hard to tell something without seeing the code, even small bug/design choice can sometime give unpredictable result.
Check my source code editor for GLBasic - link Update: 20.04.2020

Ian Price

It shouldn't be slow at all - I generated 9x9 puzzles for my 2016 Sudoku WIP (LOL - no work, no progress!) in less than a second, which uses arrays and checking.

Unfortunately I no longer have GLBasic on my new laptop, so can't share the code.

Here's a link to the WIP though - https://socoder.net/?Topic=5728

I also did Towers and KenKen (Ken2X) games for the Palm Pre/Pixi etc. and they were fast too, even on limited hardware.

BTW I love those 1/0 puzzles. :D
I came. I saw. I played.

Kitty Hello

Most of the times its your algorithm. Invert a loop here, use an array instead of a loop there...


thank you all for your answers. My generator needed around 300-000 and 2.million attempts to randomly create a binary puzzle, but when I generate for 12x12 instead of 8x8 it takes like 100 times longer (minutes). Meanwhile I have updated the program and it is working much faster. However, after I realised that i need not only a grid of 0 and 1 that is working, but also it must be ensured that the player can solve it (depending on what fields are visible at the beginning). i really lost any hope for the moment to solve this problem. Thank you all again!


Ian Price

Please do come back to this - while puzzle games aren't generally as popular as other genres, they do garner a good following and there's always room for more, especially ones like this that aren't paerticularly well known.

As for starting positions, yeah, selecting a start for a solvable puzzle is tricky. You could start by making a percentage of cells visible (or make have it varied based on player difficulty), and then create a system that can try to solve the puzzle. If it can't,  re-select the start clues.
I came. I saw. I played.