Conway's Game of Life

h3ky1
Posts: 2
Joined: Fri Jul 12, 2013 4:00 am

Conway's Game of Life

Post by h3ky1 »

I tried to write this first in the other popular BASIC! for iOS. While it ran, the same code ported to this platform runs 10 times faster. Yes, 10 times faster--you will see the generation timer in this program. My phone runs each generation around 0.180 seconds. The other BASIC interpreter was running at around 2 seconds per generation. My guess is that Smart BASIC has better hooks into the iOS graphics API.

By the way, this code is short enough to run on the Lite/free version of Smart BASIC as well.

Code: Select all

REM Game of Life by h3ky1 2013
GRAPHICS ! RANDOMIZE ! s=10
maxx = INT(screen_width()/s)
maxy = INT(screen_height()/s)
DIM cello(maxx,maxy), celln(maxx,maxy)
FOR i=1 TO maxx*maxy*0.1
    cello(RND(maxx),RND(maxy))=1
NEXT i
loop: ! g=g+1
GRAPHICS LOCK ! GRAPHICS CLEAR
FOR x=0 TO maxx-1
    FOR y=0 TO maxy-1
        IF cello(x,y)=1 THEN
            FILL COLOR 1,0,0
            FILL CIRCLE x*s,y*s SIZE s/2
        END IF
    NEXT y
NEXT x
t2=timer()
DRAW TEXT "G=" & STR$(g) & " T=" & STR$((t2-t1)/1000) AT 1,1
t1=timer()
GRAPHICS UNLOCK
FOR x=0 TO maxx-1
    FOR y=0 TO maxy-1
        n = 0
        x0 = x-1
        x1 = x+1
        y0 = y-1
        y1 = y+1
        IF x0<0 THEN
            x0=maxx-1
        END IF
        IF x1>maxx-1 THEN
            x1=0
        END IF
        IF y0<0 THEN
            y0=maxy-1
        END IF
        IF y1>maxy-1 THEN
            y1=0
        END IF
        n=cello(x0,y0) + cello(x0,y) + cello(x0,y1) + cello(x,y0) + cello(x,y1) + cello(x1,y0) + cello(x1,y) + cello(x1,y1)
        IF cello(x,y)=1 THEN
            IF n<2 OR n>3 THEN
                celln(x,y)=0
            ELSE
                celln(x,y)=1
            END IF 
        END IF
        IF cello(x,y)=0 THEN
            IF n=3 THEN
                celln(x,y)=1
            ELSE
                celln(x,y)=0
            END IF
        END IF
    NEXT y
NEXT x
FOR x=0 TO maxx-1
    FOR y=0 TO maxy-1
        IF cello(x,y) <> celln(x,y) THEN 
            cello(x,y) = celln(x,y)
        END IF
    NEXT y
NEXT x
GOTO loop

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Conway's Game of Life

Post by Mr. Kibernetik »

Did I understand you correctly, that smart BASIC runs the same code 10 times faster than BASIC! ?

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Conway's Game of Life

Post by Henko »

Mr. Kibernetik wrote:Did I understand you correctly, that smart BASIC runs the same code 10 times faster than BASIC! ?
It doesn't surprise me.
I did a 9x9 matrix inversion (exclusively floating point arithmetic) using the inversion routine that i wrote for both basics (check the program sections). Using the timer:
Basic! : 580 msec
Smart Basic : 18 msec , hence about 32x faster.

So, it's not only the graphics, but even more so the calculations.

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Conway's Game of Life

Post by Mr. Kibernetik »

Henko wrote:
Mr. Kibernetik wrote:Did I understand you correctly, that smart BASIC runs the same code 10 times faster than BASIC! ?
It doesn't surprise me.
I did a 9x9 matrix inversion (exclusively floating point arithmetic) using the inversion routine that i wrote for both basics (check the program sections). Using the timer:
Basic! : 580 msec
Smart Basic : 18 msec , hence about 32x faster.

So, it's not only the graphics, but even more so the calculations.
Heh, my iPad (1st gen) runs this program 2 sec each generation... :?

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Conway's Game of Life

Post by Mr. Kibernetik »

Well, this is my modification of the program by h3ky1.
It runs faster and is a bit more funny to look at:

Code: Select all

' Game of Life by h3ky1 2013
' mod by Mr.K
GRAPHICS ! RANDOMIZE
s=10
maxx = INT(screen_width()/s)
maxy = INT(screen_height()/s)
maxx1 = maxx-1
maxy1 = maxy-1
DIM cello(maxx,maxy), celln(maxx,maxy), age(maxx,maxy)
FOR i=1 TO maxx*maxy/s
    x=RND(maxx)
    y=RND(maxy)
    cello(x,y)=1
    age(x,y)=1
NEXT i

loop: g=g+1
GRAPHICS LOCK ! GRAPHICS CLEAR
FOR x=0 TO maxx1
    FOR y=0 TO maxy1
        IF cello(x,y) THEN
            ageColor=1/age(x,y)
            FILL COLOR ageColor,0,1-ageColor
            FILL CIRCLE x*s,y*s SIZE s/2
        ENDIF
    NEXT y
NEXT x
t2=timer()
DRAW TEXT "G=" & STR$(g) & " T=" & STR$((t2-t1)/1000) AT 1,1
t1=timer()
GRAPHICS UNLOCK
FOR x=0 TO maxx1
    x0 = x-1
    x1 = x+1
    FOR y=0 TO maxy1
        y0 = y-1
        y1 = y+1
        IF x0<0 THEN x0=maxx1
        IF x1>maxx1 THEN x1=0
        IF y0<0 THEN y0=maxy1
        IF y1>maxy1 THEN y1=0
        n=cello(x0,y0) + cello(x0,y) + cello(x0,y1) + cello(x,y0) + cello(x,y1) + cello(x1,y0) + cello(x1,y) + cello(x1,y1)
        IF cello(x,y)=1 THEN
            IF n<2 OR n>3 THEN
                celln(x,y)=0
            ELSE
                celln(x,y)=1
                age(x,y)=age(x,y)+1
            ENDIF
        ELSE
            IF n=3 THEN
                celln(x,y)=1
                age(x,y)=1
            ELSE
                celln(x,y)=0
            ENDIF
        END IF
    NEXT y
NEXT x
FOR x=0 TO maxx1
    FOR y=0 TO maxy1
        cello(x,y) = celln(x,y)
    NEXT y
NEXT x
GOTO loop

h3ky1
Posts: 2
Joined: Fri Jul 12, 2013 4:00 am

Re: Conway's Game of Life

Post by h3ky1 »

Thanks for the two speed ups: moving the x calc up into just the x for-loop (not both x and y for-loops; and moving the minus 1 calc out of the for-loops into constants. I like the aging colour because now it is obvious when the game stabilizes or oscillates forever. The code is a little long now for a Lite version sample, so I modified it a bit to bring it back into that constraint, by putting some calcs on the DIMs and setup while leaving them out of the for-loops.

Code: Select all

' Game of Life by h3ky1 2013
' mod by Mr.K
GRAPHICS ! RANDOMIZE ! s=10
maxx = INT(screen_width()/s)-1
maxy = INT(screen_height()/s)-1
DIM cello(maxx+1,maxy+1), celln(maxx+1,maxy+1), age(maxx+1,maxy+1)
FOR i=1 TO (maxx+1)*(maxy+1)/s
    x=RND(maxx+1)
    y=RND(maxy+1)
    cello(x,y)=1
    age(x,y)=1
NEXT i
loop: g=g+1
GRAPHICS LOCK ! GRAPHICS CLEAR
FOR x=0 TO maxx
    FOR y=0 TO maxy
        IF cello(x,y) THEN
            ageColor=1/age(x,y)
            FILL COLOR ageColor,0,1-ageColor
            FILL CIRCLE x*s,y*s SIZE s/2
        ENDIF
    NEXT y
NEXT x
t2=timer()
DRAW TEXT "G=" & STR$(g) & " T=" & STR$((t2-t1)/1000) AT 1,1
t1=timer()
GRAPHICS UNLOCK
FOR x=0 TO maxx
    x0 = x-1
    x1 = x+1
    FOR y=0 TO maxy
        y0 = y-1
        y1 = y+1
        IF x0<0 THEN x0=maxx
        IF x1>maxx THEN x1=0
        IF y0<0 THEN y0=maxy
        IF y1>maxy THEN y1=0
        n=cello(x0,y0) + cello(x0,y) + cello(x0,y1) + cello(x,y0) + cello(x,y1) + cello(x1,y0) + cello(x1,y) + cello(x1,y1)
        IF cello(x,y)=1 THEN
            IF n<2 OR n>3 THEN
                celln(x,y)=0
            ELSE
                celln(x,y)=1
                age(x,y)=age(x,y)+1
            ENDIF
        ELSE
            IF n=3 THEN
                celln(x,y)=1
                age(x,y)=1
            ELSE
                celln(x,y)=0
            ENDIF
        END IF
    NEXT y
NEXT x
FOR x=0 TO maxx
    FOR y=0 TO maxy
        cello(x,y) = celln(x,y)
    NEXT y
NEXT x
GOTO loop

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Conway's Game of Life

Post by Mr. Kibernetik »

Next speed modification :)
FOR Y cycle can be optimized even more. Timing functions are commented out here to make it faster.

Code: Select all

' Game of Life by h3ky1 2013
' mod by Mr.K
GRAPHICS ! RANDOMIZE ! s=10
maxx = INT(screen_width()/s)-1
maxy = INT(screen_height()/s)-1
DIM cello(maxx+1,maxy+1), celln(maxx+1,maxy+1), age(maxx+1,maxy+1)
FOR i=1 TO (maxx+1)*(maxy+1)/s
    x=RND(maxx+1)
    y=RND(maxy+1)
    cello(x,y)=1
    age(x,y)=1
NEXT i
loop: g=g+1
GRAPHICS LOCK ! GRAPHICS CLEAR
FOR x=0 TO maxx
    FOR y=0 TO maxy
        IF cello(x,y) THEN
            ageColor=1/age(x,y)
            FILL COLOR ageColor,0,1-ageColor
            FILL CIRCLE x*s,y*s SIZE s/2
        ENDIF
    NEXT y
NEXT x
't2=timer()
DRAW TEXT "Gen " & STR$(g) AT 4,1' & ", T=" & STR$((t2-t1)/1000) AT 1,1
't1=timer()
GRAPHICS UNLOCK
FOR x=0 TO maxx
    x0 = x-1
    x1 = x+1
    IF x0<0 THEN x0=maxx
    IF x1>maxx THEN x1=0
    FOR y=0 TO maxy
        y0 = y-1
        y1 = y+1
        IF y0<0 THEN y0=maxy
        IF y1>maxy THEN y1=0
        n=cello(x0,y0) + cello(x0,y) + cello(x0,y1) + cello(x,y0) + cello(x,y1) + cello(x1,y0) + cello(x1,y) + cello(x1,y1)
        IF cello(x,y)=1 THEN
            IF n<2 OR n>3 THEN
                celln(x,y)=0
            ELSE
                celln(x,y)=1
                age(x,y)=age(x,y)+1
            ENDIF
        ELSE
            IF n=3 THEN
                celln(x,y)=1
                age(x,y)=1
            ELSE
                celln(x,y)=0
            ENDIF
        END IF
    NEXT y
NEXT x
FOR x=0 TO maxx
    FOR y=0 TO maxy
        cello(x,y) = celln(x,y)
    NEXT y
NEXT x
GOTO loop

Dalede
Posts: 131
Joined: Fri Dec 28, 2012 4:00 pm
Location: Grass Valley, CA, USA
Contact:

Re: Conway's Game of Life

Post by Dalede »

I believe the reason for the better speed in this basic is that variable value are stored as binary numbers similar to the ECMA standard and the way they were done in Microsoft basics of olden days. While Basic! stores numbers in some sort of binary code decimal form instead of pure binary. Also Basic! does have a bit more precision than Smart Basic. I would really like a 4 byte mantissa like the old Commodore basic had with about 8.5 digits of accuracy instead of the current 6.5 digits.

Dale

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Conway's Game of Life

Post by Mr. Kibernetik »

Dalede wrote:I believe the reason for the better speed in this basic is that variable value are stored as binary numbers similar to the ECMA standard and the way they were done in Microsoft basics of olden days. While Basic! stores numbers in some sort of binary code decimal form instead of pure binary. Also Basic! does have a bit more precision than Smart Basic. I would really like a 4 byte mantissa like the old Commodore basic had with about 8.5 digits of accuracy instead of the current 6.5 digits.

Dale
smart BASIC is designed to be fast. It is developed and tuned with intention of highest speed performance.

Precision of variables in smart BASIC is 8 bytes, or double in objective c syntax. Mantissa is 52 bit long in this float number representation. I am not sure what you mean under 6.5 digits of accuracy here.

You can easily check precision with, for example:

Code: Select all

PRINT "#.###############": SQR(2)
which gives output 1.414213562373095

or:

Code: Select all

PRINT "# ### ### ### ### ###": 2^52
which gives 4 503 599 627 370 496

Dalede
Posts: 131
Joined: Fri Dec 28, 2012 4:00 pm
Location: Grass Valley, CA, USA
Contact:

Re: Conway's Game of Life

Post by Dalede »

Thanks for the info on precision. The using function is good for seeing what the real precision is. I was basing on the ECMA but this basic is no longer very close to ECMA particularly as far as it relates to limitations. 8 bytes mantissa would be 64 bits (8 x8). Do you mean the entire floating point number is 64 bits with 52 bits related to the mantissa and the rest to the exponent?

Dale

Post Reply