Author Topic: How can this be sped up?  (Read 4308 times)

0 Members and 1 Guest are viewing this topic.

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
How can this be sped up?
« on: December 25, 2019, 12:03:36 am »
This is just one of many rules (the simplest) in a "rule based transformation" engine that I wrote about 30 some years ago. I've always wanted to see the results on a higher resolution monitor. So I'm wondering if this example can be made faster.

It is a very interesting thing to watch. Some rule sets results in patterns that resemble a living system. Other rule sets produce something like Native American patterns such as they wove in their clothing. The possible rule sets are limitless. The name I gave it way back when was Evolve.

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. WHILE repeat
  16.  
  17.     x = RND * screenw
  18.     y = RND * screenh
  19.     c = POINT(x, y)
  20.     eat = c + 1
  21.     IF eat > 15 THEN eat = 0
  22.     FOR i = x - 1 TO x + 1
  23.         FOR j = y - 1 TO y + 1
  24.             c = POINT(i, j)
  25.             IF c = eat THEN
  26.                 PSET (x, y), c
  27.                 EXIT FOR
  28.             END IF
  29.         NEXT
  30.         IF c = eat THEN EXIT FOR
  31.     NEXT
  32.     ch = INKEY$
  33.     IF ch = CHR$(27) THEN repeat = 0
  34.     _DISPLAY
  35.  
My name is Michael, but you can call me Mike :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How can this be sped up?
« Reply #1 on: December 25, 2019, 12:12:42 am »
DO instead of FOR, _MEM instead of POINT and PSET.  Change to DEFLNG instead of default SINGLE precision.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: How can this be sped up?
« Reply #2 on: December 25, 2019, 01:29:51 am »
More interesting though not that much faster:
Code: QB64: [Select]
  1.  
  2. screenw = 100
  3. screenh = 100
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5. '_FULLSCREEN
  6.  
  7. FOR y = 0 TO screenh
  8.     FOR x = 0 TO screenw
  9.         clr = INT(RND * 211)
  10.         PSET (x, y), clr
  11.     NEXT
  12.     _DISPLAY
  13.  
  14. repeat = -1
  15.  
  16. WHILE repeat
  17.  
  18.     x = RND * screenw
  19.     y = RND * screenh
  20.     c = POINT(x, y)
  21.     eat = c + 1
  22.     IF eat > 15 THEN eat = 0
  23.     FOR i = x - 1 TO x + 1
  24.         FOR j = y - 1 TO y + 1
  25.             c = POINT(i, j)
  26.             IF c = eat THEN
  27.                 PSET (x, y), c
  28.                 GOTO skip
  29.             END IF
  30.         NEXT
  31.         'IF c = eat THEN EXIT FOR
  32.     NEXT
  33.     skip:
  34.     'ch = INKEY$
  35.     'IF ch = CHR$(27) THEN repeat = 0
  36.     _DISPLAY
  37.  
  38.  
  39.  

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #3 on: December 25, 2019, 01:31:18 am »
DO instead of FOR, _MEM instead of POINT and PSET.  Change to DEFLNG instead of default SINGLE precision.

Thanks! I get started on it tomorrow. :)
My name is Michael, but you can call me Mike :)

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #4 on: December 25, 2019, 04:17:13 am »
Well looks like I'm up late again. My original Evolve engine started just about like the code that was posted. I'd like to describe where it was when it ended. It ended because all I had to work with was an 80386 50MHz machine and it just didn't have the computing power needed. It started off single pixel like the sample code. At the end I created 2x2 pixel entities. Just a few. After so many random pixels were plotted a scan would be made to look for new entities that had formed. They were stored in an array. They were immutable by random pixels. But the entities had rules of their own. They could bond, repel, move, destroy other entities. They formed chains. It was pseudo chemistry. However, with every random pixel having to be checked against the growing list of entities and checking for all the possibilities between entities quickly brought the simulation to a virtual standstill. So the idea was abandoned. One can imagine the possibilities! :)
My name is Michael, but you can call me Mike :)

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #5 on: December 25, 2019, 04:36:03 am »
Here is a better version that produces more consistent results. It is something that one does when they have other things to do and can check back every so often. It takes a while to evolve. I took a look at _mem and it was at first look boggling to my old brain. Maybe someone will champion the conversion? Oh, and post it, lol. :) Back to bed for another try at sleeping!

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. WHILE repeat
  16.  
  17.     x = RND * screenw
  18.     y = RND * screenh
  19.     c = POINT(x, y)
  20.     eat1 = c + 1
  21.     eat2 = c + 2
  22.     IF eat1 > 15 THEN
  23.         eat1 = 0
  24.         eat2 = 1
  25.     ELSE
  26.         IF eat2 > 16 THEN
  27.             eat2 = 0
  28.         END IF
  29.     END IF
  30.  
  31.     FOR i = x - 1 TO x + 1
  32.         FOR j = y - 1 TO y + 1
  33.             c = POINT(i, j)
  34.             IF c = eat1 OR c = eat2 THEN
  35.                 PSET (x, y), c
  36.                 EXIT FOR
  37.             END IF
  38.         NEXT
  39.         IF c = eat1 OR c = eat2 THEN EXIT FOR
  40.     NEXT
  41.     ch = INKEY$
  42.     IF ch = CHR$(27) THEN repeat = 0
  43.     _DISPLAY
  44.  
  45.  
« Last Edit: December 25, 2019, 04:39:56 am by romichess »
My name is Michael, but you can call me Mike :)

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #6 on: December 25, 2019, 04:57:19 am »
I have become such a dunce in my old age, LOL. When I was getting into bed it hit me, "Why am I calling _DISPLAY AFTER EVERY SINGLE PIXEL, DUH! Run this simple one rule example and tell me your not amazed!!!?

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. WHILE repeat
  16.     FOR h = 1 TO 32000
  17.         x = RND * screenw
  18.         y = RND * screenh
  19.         c = POINT(x, y)
  20.         eat1 = c + 1
  21.         eat2 = c + 2
  22.         IF eat1 > 15 THEN
  23.             eat1 = 0
  24.             eat2 = 1
  25.         ELSE
  26.             IF eat2 > 16 THEN
  27.                 eat2 = 0
  28.             END IF
  29.         END IF
  30.  
  31.         FOR i = x - 1 TO x + 1
  32.             FOR j = y - 1 TO y + 1
  33.                 c = POINT(i, j)
  34.                 IF c = eat1 OR c = eat2 THEN
  35.                     PSET (x, y), c
  36.                     EXIT FOR
  37.                 END IF
  38.             NEXT
  39.             IF c = eat1 OR c = eat2 THEN EXIT FOR
  40.         NEXT
  41.     NEXT
  42.     _DISPLAY
  43.     ch = INKEY$
  44.     IF ch = CHR$(27) THEN repeat = 0
  45.  
  46.  
My name is Michael, but you can call me Mike :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How can this be sped up?
« Reply #7 on: December 25, 2019, 05:03:51 am »
The original, with a FPS counter printed to the top left of it:


Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. t# = TIMER + 1
  16. WHILE repeat
  17.     fps = fps + 1
  18.     x = RND * screenw
  19.     y = RND * screenh
  20.     c = POINT(x, y)
  21.     eat = c + 1
  22.     IF eat > 15 THEN eat = 0
  23.     FOR i = x - 1 TO x + 1
  24.         FOR j = y - 1 TO y + 1
  25.             c = POINT(i, j)
  26.             IF c = eat THEN
  27.                 PSET (x, y), c
  28.                 EXIT FOR
  29.             END IF
  30.         NEXT
  31.         IF c = eat THEN EXIT FOR
  32.     NEXT
  33.     ch = INKEY$
  34.     IF ch = CHR$(27) THEN repeat = 0
  35.     IF TIMER > t# THEN fpsdisplay = fps: fps = 0: t# = TIMER + 1
  36.     LOCATE 1, 1: PRINT "FPS:"; fpsdisplay
  37.     _DISPLAY
  38.  
  39.  
  40.  

A _MEM version, with a FPS counter printed to the top left of it:

Code: QB64: [Select]
  1. DEFLNG A-Z
  2.  
  3. screenw = _DESKTOPWIDTH
  4. screenh = _DESKTOPHEIGHT
  5. SCREEN _NEWIMAGE(screenw, screenh, 256)
  6. m = _MEMIMAGE(0) 'visible screen memory
  7.  
  8. o = 0
  9.     _MEMPUT m, m.OFFSET + o, INT(RND * 16) AS _UNSIGNED _BYTE
  10.     o = o + 1
  11. LOOP UNTIL o >= m.SIZE
  12.  
  13. repeat = -1
  14.  
  15. screenw = screenw - 3: screenh = screenh - 3
  16. t# = TIMER + 1
  17. WHILE repeat
  18.     fps = fps + 1
  19.     x = RND * screenw + 1
  20.     y = RND * screenh + 1
  21.     c = _MEMGET(m, m.OFFSET + y * screenw + x, _UNSIGNED _BYTE)
  22.     eat = (c + 1) MOD 15
  23.     i = x - 1
  24.     DO
  25.         j = y - 1
  26.         DO
  27.             c = _MEMGET(m, m.OFFSET + j * screenw + i, _UNSIGNED _BYTE)
  28.             IF c = eat THEN
  29.                 _MEMPUT m, m.OFFSET + y * screenw + x, c
  30.                 GOTO skip
  31.             END IF
  32.             j = j + 1
  33.         LOOP UNTIL j > y + 1
  34.         i = i + 1
  35.     LOOP UNTIL i > x + 1
  36.     skip:
  37.     IF _KEYDOWN(27) THEN repeat = 0
  38.     IF TIMER > t# THEN fpsdisplay = fps: fps = 0: t# = TIMER + 1
  39.     _PRINTSTRING (0, 0), STR$(fpsdisplay)
  40.  
  41.  

There's just a slight difference between the two versions, with the original doing about 400 - 500 FPS on my PC, and the _MEM version only doing a little better at 800,000 - 1,000,000 FPS. :D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #8 on: December 25, 2019, 06:36:28 am »
The original, with a FPS counter printed to the top left of it:


Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. t# = TIMER + 1
  16. WHILE repeat
  17.     fps = fps + 1
  18.     x = RND * screenw
  19.     y = RND * screenh
  20.     c = POINT(x, y)
  21.     eat = c + 1
  22.     IF eat > 15 THEN eat = 0
  23.     FOR i = x - 1 TO x + 1
  24.         FOR j = y - 1 TO y + 1
  25.             c = POINT(i, j)
  26.             IF c = eat THEN
  27.                 PSET (x, y), c
  28.                 EXIT FOR
  29.             END IF
  30.         NEXT
  31.         IF c = eat THEN EXIT FOR
  32.     NEXT
  33.     ch = INKEY$
  34.     IF ch = CHR$(27) THEN repeat = 0
  35.     IF TIMER > t# THEN fpsdisplay = fps: fps = 0: t# = TIMER + 1
  36.     LOCATE 1, 1: PRINT "FPS:"; fpsdisplay
  37.     _DISPLAY
  38.  
  39.  
  40.  

A _MEM version, with a FPS counter printed to the top left of it:

Code: QB64: [Select]
  1. DEFLNG A-Z
  2.  
  3. screenw = _DESKTOPWIDTH
  4. screenh = _DESKTOPHEIGHT
  5. SCREEN _NEWIMAGE(screenw, screenh, 256)
  6. m = _MEMIMAGE(0) 'visible screen memory
  7.  
  8. o = 0
  9.     _MEMPUT m, m.OFFSET + o, INT(RND * 16) AS _UNSIGNED _BYTE
  10.     o = o + 1
  11. LOOP UNTIL o >= m.SIZE
  12.  
  13. repeat = -1
  14.  
  15. screenw = screenw - 3: screenh = screenh - 3
  16. t# = TIMER + 1
  17. WHILE repeat
  18.     fps = fps + 1
  19.     x = RND * screenw + 1
  20.     y = RND * screenh + 1
  21.     c = _MEMGET(m, m.OFFSET + y * screenw + x, _UNSIGNED _BYTE)
  22.     eat = (c + 1) MOD 15
  23.     i = x - 1
  24.     DO
  25.         j = y - 1
  26.         DO
  27.             c = _MEMGET(m, m.OFFSET + j * screenw + i, _UNSIGNED _BYTE)
  28.             IF c = eat THEN
  29.                 _MEMPUT m, m.OFFSET + y * screenw + x, c
  30.                 GOTO skip
  31.             END IF
  32.             j = j + 1
  33.         LOOP UNTIL j > y + 1
  34.         i = i + 1
  35.     LOOP UNTIL i > x + 1
  36.     skip:
  37.     IF _KEYDOWN(27) THEN repeat = 0
  38.     IF TIMER > t# THEN fpsdisplay = fps: fps = 0: t# = TIMER + 1
  39.     _PRINTSTRING (0, 0), STR$(fpsdisplay)
  40.  
  41.  

There's just a slight difference between the two versions, with the original doing about 400 - 500 FPS on my PC, and the _MEM version only doing a little better at 800,000 - 1,000,000 FPS. :D

Wow, thanks! These are things I needed to learn.
My name is Michael, but you can call me Mike :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How can this be sped up?
« Reply #9 on: December 25, 2019, 06:39:30 am »
Let me point you over to some nice holiday movies to watch, and see if they don't take the mystery out of the commands for you: https://www.qb64.org/forum/index.php?topic=1731.0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #10 on: December 25, 2019, 07:12:16 am »
Let me point you over to some nice holiday movies to watch, and see if they don't take the mystery out of the commands for you: https://www.qb64.org/forum/index.php?topic=1731.0

Ok cool. I'll watch them tomorrow. There is an error in the Do _mem version. I can't quite pinpoint what it is. And the timer does come with a big performance hit. Not a complaint. I appreciate the instruction! If you want to see what the original should look like then run this. It is really very appealing to watch. And it runs acceptably fast with _DISPLAY only called every 32k pixels.

Code: QB64: [Select]
  1.  
  2. screenw = _DESKTOPWIDTH
  3. screenh = _DESKTOPHEIGHT
  4. SCREEN _NEWIMAGE(screenw, screenh, 256)
  5.  
  6. FOR y = 0 TO screenh
  7.     FOR x = 0 TO screenw
  8.         clr = INT(RND * 16)
  9.         PSET (x, y), clr
  10.     NEXT
  11.     _DISPLAY
  12.  
  13. repeat = -1
  14.  
  15. 't# = TIMER + 1
  16. WHILE repeat
  17.     FOR h = 1 TO 32000
  18.         fps = fps + 1
  19.         x = RND * screenw
  20.         y = RND * screenh
  21.         c = POINT(x, y)
  22.         eat = c + 1
  23.         IF eat > 15 THEN eat = 0
  24.         FOR i = x - 1 TO x + 1
  25.             FOR j = y - 1 TO y + 1
  26.                 c = POINT(i, j)
  27.                 IF c = eat THEN
  28.                     PSET (x, y), c
  29.                     EXIT FOR
  30.                 END IF
  31.             NEXT
  32.             IF c = eat THEN EXIT FOR
  33.         NEXT
  34.         ch = INKEY$
  35.         IF ch = CHR$(27) THEN repeat = 0
  36.         '        IF TIMER > t# THEN fpsdisplay = fps: fps = 0: t# = TIMER + 1
  37.         '        LOCATE 1, 1: PRINT "FPS:"; fpsdisplay
  38.     NEXT
  39.  
  40.     _DISPLAY
  41.  
  42.  
  43.  
  44.  
My name is Michael, but you can call me Mike :)

Offline romichess

  • Forum Regular
  • Posts: 145
    • View Profile
Re: How can this be sped up?
« Reply #11 on: December 25, 2019, 03:46:41 pm »
I'm starting a new thread in programs on this, "Evolve Engine".
My name is Michael, but you can call me Mike :)