LoginLogin

Speed optimizations

Root / Submissions / [.]

DFrostCreated:
One problem with SB: speed. SB lacks speed. In this "tutorial" i will show you ways to make your code faster/more efficient

One example

Here is one example:
DIM MAP[100,100]
FOR I = 0 TO 99
 FOR J=0 TO 99
 MAP[I,J] = RND(100)
 NEXT
NEXT
or you can do this:
DIM MAP[100,100]
FOR CNT = 0 TO 99*99
 I = FLOOR(CNT/99)
 J = CNT MOD 99
 MAP[I,J]=RND(100)
NEXT
the reason why it is faster is because it is slower to make nested loops

DON'T BE LAZY!

instead of this:
FOR I = 0 TO 20
 PRINT I
NEXT
do this:
PRINT 1
PRINT 2
PRINT 3
PRINT 4
PRINT 5
PRINT 6
PRINT 7
PRINT 8
PRINT 9
PRINT 10
PRINT 11
PRINT 12
PRINT 13
PRINT 14
PRINT 15
PRINT 16
PRINT 17
PRINT 18
PRINT 19
PRINT 20

another method

if you have a main loop(a game, for example) and you want to update the map inside of it, do this:
DIM MAP[W,H]
VAR I,J
WHILE 1
 'code and all that
 INC I
IF J == H-1 && I==W-1 THEN I = 0 : J = 0
 IF I==W-1 THEN INC J:I=0
 DOSOMETHING I,J'do something to the map, like set it as an empty tile for BG
WEND
this method is going to update the map slowly, but it will not cause the main loop to have a low FPS

Use VSYNC conditionally

WHILE 1
M=MILLISEC
IF B THEN CLS'AND OTHER STUFF
B=!B
'DO SOMETHING
IF MILLISEC-M>30.3030303 THEN VSYNC'30.3030303 milliseconds is 30 FPS. if fps is greater than 30,VSYNC to slow it down a bit
WEND
I will add more later

- Dont be lazy - I will add more later hmm

Replying to:MochaProbably
- Dont be lazy - I will add more later hmm
lol

Dfrost i did testing with your methods. No nested FOR loops are faster (with both loops doing the equivalent of 1999*1999 nested was almost 100ms faster) And VSYNC can put any program at 30FPS consistently by using VSYNC 2.

Replying to:MochaProbably
- Dont be lazy - I will add more later hmm
this comment is why we need to be able to upvote comments lmao

Replying to:MochaProbably
- Dont be lazy - I will add more later hmm
lol x2

are you trolling?

Replying to:MochaProbably
- Dont be lazy - I will add more later hmm
DEF laugh OUT LOUD
LOUD = POW(1000,1000)'returns inf
END
DEF LOL
LAUGH OUT LOUD
END
LOL!

Replying to:MochaProbably
- Dont be lazy - I will add more later hmm
also, I was doing this in a pinch of time(like I am now)