We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
LIFE GAME : How to optimise/speed up ?
#3
Hi again,

The main reason is that Small Basic arrays are slow and 2D array can be very slow - this was an internal design decision that arises from SB having no types (int, float string etc).

Not so much you can do about it unless you use an extension as ZS suggests.  I have no idea if you do or not, or want to consider this.

Anyway I did a little test to show:

Code:
size = 100
T1 = Clock.ElapsedMilliseconds
For x = 1 To size
  For y = 1 To size
    rand = Math.GetRandomNumber(100)
    If 50 > rand Then
      table1[x][y] = 1
    Else
      table1[x][y] = 0
    EndIf
  EndFor
EndFor
T2 = Clock.ElapsedMilliseconds
For x = 1 To size
  For y = 1 To size
    If table1[x][y] = 1 Then
      table2[x][y]= 0
    Else
      table2[x][y]= 1
    EndIf
  EndFor
EndFor
T3 = Clock.ElapsedMilliseconds
TextWindow.WriteLine(T2-T1)
TextWindow.WriteLine(T3-T2)

On my PC (which is quite high spec) I get times of around 150 ms for each of the 2 passes.

If I code the same using a LitDev extension method LDFastArray as below, I get timings of around 8 ms for each (this is actually still not very fast due to overheads using function calls and underlying SB data types, but better).

Code:
T1 = Clock.ElapsedMilliseconds
arr1 = LDFastArray.Add()
For x = 1 To size
  For y = 1 To size
    rand = Math.GetRandomNumber(100)
    If 50 > rand Then
      LDFastArray.Set2D(arr1,x,y,1)
    Else
      LDFastArray.Set2D(arr1,x,y,0)
    EndIf
  EndFor
EndFor
T2 = Clock.ElapsedMilliseconds
arr2 = LDFastArray.Add()
For x = 1 To size
  For y = 1 To size
    If LDFastArray.Get2D(arr1,x,y) = 1 Then
      LDFastArray.Set2D(arr2,x,y,0)
    Else
      LDFastArray.Set2D(arr2,x,y,1)
    EndIf
  EndFor
EndFor
T3 = Clock.ElapsedMilliseconds
TextWindow.WriteLine(T2-T1)
TextWindow.WriteLine(T3-T2)

There are other faster array tpes LDList (for 1D arrays not indexed by integer with improved sort capabilities) or LDArray (fastest for 1D arrays with a pre-defined size).

If you are interested in these instruction for using LitDev extension can be found at https://litdev.uk and we can help futher with this.  As ZS says there may be other optimisations, but I expect the arrays are the main offenders here.
[-] The following 1 user Likes litdev's post:
  • z-s
Reply


Messages In This Thread
RE: LIFE GAME : How to optimise/speed up ? - by litdev - 06-27-2024, 03:43 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)