Author Topic: speeding up mathematical operations?  (Read 214 times)

Offline svenart

  • Mc. Print
  • *
  • Posts: 46
    • View Profile
speeding up mathematical operations?
« on: 2022-Oct-07 »
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.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1793
    • View Profile
    • Homepage
Re: speeding up mathematical operations?
« Reply #1 on: 2022-Oct-07 »
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?


Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4448
    • View Profile
    • FUED
Re: speeding up mathematical operations?
« Reply #2 on: 2022-Oct-08 »
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.

Offline dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 454
    • View Profile
    • my personal website
Re: speeding up mathematical operations?
« Reply #3 on: 2022-Oct-08 »
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

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4175
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: speeding up mathematical operations?
« Reply #4 on: 2022-Oct-10 »
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
« Last Edit: 2022-Oct-10 by Ian Price »
I came. I saw. I played.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10839
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: speeding up mathematical operations?
« Reply #5 on: 2022-Oct-12 »
Most of the times its your algorithm. Invert a loop here, use an array instead of a loop there...

Offline svenart

  • Mc. Print
  • *
  • Posts: 46
    • View Profile
Re: speeding up mathematical operations?
« Reply #6 on: 2022-Oct-14 »
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!

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4175
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: speeding up mathematical operations?
« Reply #7 on: 2022-Oct-14 »
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.